#include <iostream>
#include<vector>
#include<fstream>
#include<queue>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int main() {
vector<int> a[100002];
int dist[100002];
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 (int i = 0; i < a[u].size(); i++){
int v = a[u][i];
if ( dist[i] == -1){
dist[i] = dist[u] + 1;
q.push(v);
}
}
}
for (int i = 1; i <= N; i++) {
g << dist[i] << " ";
}
return 0;
}