#include <bits/stdc++.h>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
vector<int>dist;
vector<vector<int>>graph;
void bfs(int node)
{
queue<int>q;
q.push(node);
dist[node]=1;
while(!q.empty())
{
node=q.front();
q.pop();
for(int next:graph[node])
if(dist[next]==0)
{
dist[next]=dist[node]+1;
q.push(next);
}
}
}
int main()
{
int n, m, k, x, y;
in >> n >> m >> k;
dist.assign(n+1, 0);
graph.assign(n+1, vector<int>());
for(int i = 1; i <= m; i++)
{
in >> x >> y;
graph[x].push_back(y);
}
bfs(k);
for(int i=1; i<=n; i++)
{
out<<dist[i]-1<<' ';
}
return 0;
}