#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int N = 100005;
queue<int> q;
vector<int> a[N];
int d[N];
int n,m,s;
void bfs(int x1)
{
d[x1] = 0;
q.push(x1);
while (!q.empty())
{
int x = q.front();
q.pop();
for(auto y : a[x])
{
if(d[y] == -1)
{
q.push(y);
d[y]=d[x]+1;
}
}
}
}
int main()
{
in>>n>>m>>s;
for(int i=0; i<m; ++i)
{
int x,y;
in>>x>>y;
a[x].push_back(y);
}
for(int i=1; i<=n; ++i)
d[i] = -1;
bfs(s);
for(int i=1; i<=n; i++)
out<<d[i]<<" ";
return 0;
}