#include <fstream>
#include <vector>
#include <queue>
#include <string.h>
std::vector <int> v[100001];
int cost[100001];
bool vis[1000001];
int n;
void
go(int s)
{
std::queue <int> q;
int node;
memset(cost, -1, (n + 1) << 2);
q.push(s);
cost[s] = 0;
vis[s] = true;
while (not q.empty())
{
node = q.front();
q.pop();
for (int i = 0; i < (int)v[node].size(); ++i)
{
if (not vis[v[node][i]])
{
cost[v[node][i]] = cost[node] + 1;
vis[v[node][i]] = true;
q.push(v[node][i]);
}
}
}
}
int main()
{
int m;
int s;
int x;
int y;
std::ifstream mama("bfs.in");
std::ofstream tata("bfs.out");
mama >> n >> m >> s;
for (int i = 0; i < m; ++i)
{
mama >> x >> y;
v[x].push_back(y);
}
go(s);
for (int i = 1; i <= n; ++i)
{
tata << cost[i] << ' ';
}
return 0;
}