#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <bitset>
#include <algorithm>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<int> g[100005];
int cost[100005];
void bfs(int s)
{
queue<int> q;
q.push(s);
cost[s] = 1;
while (!q.empty())
{
int x = q.front();
q.pop();
// fout << x << " ";
for (auto i : g[x])
{
if (cost[i] == 0)
{
cost[i] = cost[x] + 1;
q.push(i);
}
}
}
}
int main()
{
int n, m, s;
fin >> n >> m >> s;
for (int i = 0; i < m; i++)
{
int a, b;
fin >> a >> b;
g[a].push_back(b);
}
bfs(s);
for (int i = 1; i <= n; i++)
fout << cost[i] - 1 << " ";
return 0;
}