#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("bfs.in");
ofstream cout("bfs.out");
int n, m, s, x, y;
int p[100001];
vector<int> a[100001];
queue<int> q;
bool use[100001];
void bfs(int s)
{
use[s] = 1;
p[s] = 0;
q.push(s);
while(!q.empty())
{
int j = q.front();
q.pop();
for(int i = 0; i < a[j].size(); i++)
{
int k = a[j][i];
if(use[k] == 0)
{
use[k] = 1;
p[k] = p[j] + 1;
q.push(k);
}
}
}
}
int main()
{
cin >> n >> m >> s;
for(int i = 1; i <= m; i++)
{
cin >> x >> y;
a[x].push_back(y);
}
for(int i = 1; i <= n; i++)
p[i] = -1;
bfs(s);
for(int i = 1; i <= n; i++)
cout << p[i] << " ";
return 0;
}