#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
vector<int> a[100002];
int dist[100002];
int main() {
int N, M, S, x, y;
f >> N >> M >> S;
for (int i = 1; i <= M; i++) {
f >> x >> y;
a[x].push_back(y);
}
for (int i = 1; i <= N; i++) {
dist[i] = -1;
}
queue<int> q;
dist[S] = 0;
q.push(S);
while (!q.empty()) {
int u = q.front();
q.pop();
for (size_t i = 0; i < a[u].size(); i++) {
int v = a[u][i];
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
for (int i = 1; i <= N; i++) {
g << dist[i] << " ";
}
return 0;
}