#define F
using namespace std;
#ifdef IO
#include <iostream>
#define fin cin
#define fout cout
#endif
#ifdef F
#include <fstream>
ifstream fin("input");
ofstream fout("output");
#endif
#include <vector>
#include <cstring>
#include <queue>
int N, M, S;
void BFS(vector <int> G[], int costs[], int startNode);
int main() {
fin >> N >> M >> S;
vector <int> G[N + 1];
for (int i = 1; i <= M; ++i) {
int x, y;
fin >> x >> y;
G[x].push_back(y);
}
int costs[N + 1];
memset(costs, -1, sizeof costs);
BFS(G, costs, S);
for (int i = 1; i <= N; ++i)
fout << costs[i] << ' ';
#ifdef F
fin.close();
fout.close();
#endif
}
void BFS(vector <int> G[], int costs[], int startNode) {
queue <int> Q;
Q.push(startNode);
costs[startNode] = 0;
while (!Q.empty()) {
int crtNode = Q.front();
Q.pop();
for (auto &it: G[crtNode]) {
if (costs[it] == -1) {
Q.push(it);
costs[it] = costs[crtNode] + 1;
}
}
}
}