#include <iostream>
#include <queue>
#include <vector>
using namespace std;
//ifstream cin("bfs.in");
//ofstream cout("bfs.out");
vector<int> G[100001];
queue<int> q;
int path[100001];
int n, m, s;
int main()
{
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
}
for (int i = 1; i <= n; i++)
path[i] = -1;
path[s] = 0;
q.push(s);
while (!q.empty()) {
int nod = q.front();
q.pop();
for (auto it : G[nod]) {
if (path[it] == -1) {
path[it] = path[nod] + 1;
q.push(it);
}
}
}
for (int i = 1; i <= n; i++)
cout << path[i] << ' ';
return 0;
}