#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
const int NMAX = 1e5+5;
vector<int> graf[NMAX];
int dist[NMAX];
void bfs(int nod) {
dist[nod] = 1;
queue<int> coada;
coada.push(nod);
while(!coada.empty()) {
nod = coada.front();
coada.pop();
for(int i = 0; i < (int)graf[nod].size(); i++) {
int next = graf[nod][i];
if(!dist[next]) {
dist[next] = dist[nod] + 1;
coada.push(next);
}
}
}
}
int main()
{
int n, m, start;
f >> n >> m >> start;
while(m--) {
int x, y;
f >> x >> y;
graf[x].push_back(y);
}
bfs(start);
for(int i = 1; i <= n; i++) {
g << dist[i] - 1 << ' ';
}
return 0;
}