#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int n, m, s, x, d[100001];
vector<int> adj[100001];
//bool viz[100001];
queue<int> q;
void bfs(int l) {
q.push(l);
d[l] = 0;
int x, dist = 0;
while(!q.empty()) {
x = q.front();
q.pop();
for(int p: adj[x]) {
if(d[p] == 0 && p != l) {
q.push(p);
d[p] = d[x]+1;
}
}
}
}
int main() {
ifstream in("bfs.in");
ofstream out("bfs.out");
in >> n >> m >> s;
for(int i = 0; i < m; i++) {
int x, y;
in >> x >> y;
adj[x].push_back(y);
}
/*for(int p: adj[1]) {
cout << p << " ";
}*/
//cout << s;
bfs(s);
for(int i = 1; i <= n; i++) {
if(d[i] > 0) {
out << d[i] << " ";
} else if(i != s){
out << -1 << " ";
} else {
out << 0 << " ";
}
}
in.close();
out.close();
return 0;
}