#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int n, m, s;
const int MAX_SIZE = 100000;
vector<int> cost, neighbors[MAX_SIZE + 1];
queue<int> nodes;
void shortest_path(int node)
{
for (int i = 0; i <= n; ++i)
{
cost.push_back(-1);
}
cost[node] = 0;
nodes.push(node);
while (!nodes.empty())
{
/*
cout << "Nodes: ";
for (auto i = nodes.begin(); i != nodes.end(); ++i) {
cout << *i << ' ';
}
cout << "\n\nCost: ";
for (int i = 1; i <= n; ++i) {
cout << '[' << i << "] " << cost[i] << ' ';
}
cout << "\n\n";
*/
int act_node = nodes.front();
int neighbors_num = neighbors[act_node].size();
nodes.pop();
for (int i = 0; i < neighbors_num; ++i)
{
int act_neighbor = neighbors[act_node][i];
if (cost[act_neighbor] == -1)
{
//cout << *i << endl;
nodes.push(act_neighbor);
cost[act_neighbor] = cost[act_node] + 1;
}
}
}
}
int main()
{
ifstream fin("bfs.in");
ofstream fout("bfs.out");
fin >> n >> m >> s;
for (int i = 0; i < m; ++i)
{
int x, y;
fin >> x >> y;
neighbors[x].push_back(y);
}
shortest_path(s);
for (int i = 1; i <= n; ++i)
{
fout << cost[i] << ' ';
}
return 0;
}