#include <bits/stdc++.h>
#include <fstream>
using namespace std;
const char iname[] = "a.in";
const char oname[] = "a.out";
#define INF (1 << 30)
int main() {
ifstream in(iname);
static constexpr int NMAX = (int)1e5 + 5;
int N, M, S;
in >> N >> M >> S;
vector<int> adj[NMAX];
for (int i = 1, x, y; i <= M; ++i) {
in >> x >> y;
adj[x].push_back(y);
}
in.close();
vector<int> dist(N + 1, INF);
queue<int> q;
dist[S] = 0;
q.push(S);
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto& neigh : adj[node]) {
int newDist = dist[node] + 1;
if (newDist < dist[neigh]) {
dist[neigh] = newDist;
q.push(neigh);
}
}
}
ofstream out(oname);
for (int i = 1; i <= N; ++i) {
if (dist[i] == INF) {
dist[i] = -1;
}
out << dist[i] << ' ';
}
out.close();
return 0;
}