#include <iostream>
#include <fstream>
#include <queue>
#include <list>
#include <bitset>
#define NMAX 100005
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s, dist[NMAX] = {0};
queue<int> Q;
list<int> vecini[NMAX];
bitset<NMAX> viz;
void bfs(int s)
{
viz[s] = 1;
Q.push(s);
while (!Q.empty())
{
for (auto node : vecini[Q.front()])
{
if (viz[node] == 0)
{
viz[node] = 1;
dist[node] = dist[Q.front()] + 1;
Q.push(node);
}
}
Q.pop();
}
}
int main()
{
cin >> n >> m >> s;
for (int i = 1; i <= m; i++)
{
int x, y;
cin >> x >> y;
vecini[x].push_back(y);
}
bfs(s);
for (int i = 1; i <= n; i++)
{
if (dist[i] == 0 && i != s)
cout << "-1 ";
else
cout << dist[i] << " ";
}
}