#include<bits/stdc++.h>
#define INF 1e9
using namespace std;
vector<vector<int>> adj;
vector<int> dist;
void bfs(int start){
queue<int> q;
q.push(start);
dist[start] = 0;
while(!q.empty()){
int poppedNode = q.front();
q.pop();
for(int i = 0; i < adj[poppedNode].size(); i++){
int nextNode = adj[poppedNode][i];
if (dist[poppedNode]+1 < dist[nextNode]){
dist[nextNode] = dist[poppedNode]+1;
q.push(nextNode);
}
}
}
}
int main(){
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s;
fin >> n >> m >> s;
adj.resize(n+5);
dist.assign(n+5, INF);
for(int i = 0; i < m; i++){
int x, y;
fin >> x >> y;
adj[x].push_back(y);
}
bfs(s);
for(int i = 1; i <= n; i++){
if (dist[i] == INF)
dist[i] = -1;
fout << dist[i] << " ";
}
return 0;
}