#include<fstream>
#include<queue>
#include<vector>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
vector <int> L[100001];
int drum[100001];
int n,m,s;
///retin graful orientat
///fac bfs in el
///nr minim de arce care trebuie vizitate de la s la fiecare nod, adica drum
void bfs(int s) {
for (int i = 1; i <= n; i++) {
drum[i] = -1;
}
queue<int>q;
q.push(s);
drum[s] = 0;
while (q.empty()==0) {
int nod = q.front();
//out << nod << " ";
q.pop();
for (auto x : L[nod]){
if (drum[x] == -1) {
q.push(x);
drum[x] = drum[nod] + 1;
}
}
}
}
int main() {
in >> n >> m >> s;
for (int i = 0; i < m; i++) {
int a, b;
in >> a >> b;
L[a].push_back(b);
}
bfs(s);
for (int i = 1; i <= n; i++) {
out << drum[i] << " ";
}
return 0;
}