Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

func pop(q *[]int) int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

C++: #include<iostream>
#include<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;

class Queue{
private:
  int inf[MAX];
  int head,tail;
 
public:
 
  Queue(){
    syokika();
  }
 
  void syokika(){
    head = 0;
    tail = 0;
  }
 
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
 
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
 
  int front(){
    return inf[head];
  }
 
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
 
  int size(){
    return tail - head;
  }
 
};

int imap[200][200];
int k[200] = {0}; 
int ans[200] = {0};
bool used[200];

int main(){
  int n,u[1000];
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF;  
  for(int i=0;i<n;i++){
    cin >> u[i];
    cin >> k[u[i]];
    for(int j=0;j<k[u[i]];j++){
      cin >> imap[u[i]][j];
    }
  }

   int h;
  for(int i=0;i<n;i++){
    int Min = i;
    for(int j=i;j<n;j++){
      if(u[Min] > u[j])Min = j; 
    }
    h = u[Min];
    u[Min] = u[i];
    u[i] = h;
  }

  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<k[u[i]];j++){
      cout << imap[u[i]][j] << " ";
    }
    cout << endl;
  }
  */

  Queue que = Queue();

  que.push(u[0]);

  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
	que.push(imap[p][i]);//cout << "push!= " << imap[p][i] << endl;
	used[imap[p][i]] = true;
	ans[imap[p][i]] = ans[p] + 1;
      }
    }
  }
  for(int i=0;i<n;i++)cout << u[i] << endl;

 
  

 
  for(int i=0;i<n;i++){
    cout << u[i] << " " << ans[u[i]] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> G[n];
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            v--;
            G[u].push_back(v);
        }
    }

    vector<bool> visited(n, false);
    vector<int> d(n, -1);
    queue<int> que;
    que.push(0);
    visited[0] = true;
    d[0] = 0;

    while (!que.empty()) {
        int u = que.front(); que.pop();
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i];
            if (!visited[v]) {
                que.push(v);
                visited[v] = true;
                d[v] = d[u] + 1;
            }
        }
    }

    for (int i = 0; i < n; i++)
        cout << i + 1 << " " << d[i] << endl;
    return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N];
intd  d[N];

void bfs(int s)  {
  queue<int> q;
  q.push(s);
  for (int i = 0; i< n; i++)  {
    d[i] = INFTY;
  }
  d[s] = 0;
  int u;
  while (!q.empty())  {
    u = q.front(); q.pop();
    for (int v = 0; v < n; v++)  {
      if (M[u][v] == 0)
        continue;
      if (d[v] != INFTY)
        continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for (int i = 0; i < n; i++)  {
    cout << i + 1 << " " << (d[i] == INFTY) ? (-1) : d[i] ) << endl;
  }
}

int main() {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++)  {
    for (int j = 0; j < n; j++)  {
      M[i][j] = 0;
    }
  }
  
  for (int i = 0; i < n; i++)  {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++)  {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  return 0;
}

C++: #include<iostream>
using namespace std;

#define N 101

void BreadthFirstSearch();

class queue{
public:
    int array[100];
    int head = -1;
    int tail = -1;
    
    void push(int v) {
        tail = (tail + 1) % N;
        array[tail] = v;
        return;
    }
    
    int pop() {
        head = (head + 1) % N;
        return array[head];
    }
    
    int show() {
        return array[head];
    }
    
    bool empty() {
        if(head == tail) return true;
        else return false;
    }
};

int n;
int d;
int a[N][N];
queue q;
int list[N];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>n;
    int u, k, v;
    for(int i = 0; i < n; i++) {
        cin>>u>>k;
        
        int c = 0;
        for(int j = 0; j < k; j++) {
            cin>>v;
            a[u][c] = v;
            c++;
        }
    }
    
    for(int i = 1; i <= n; i++) list[i] = -1;
    
    q.push(1);
    list[1] = 0;
    while(!q.empty()) {
        d++;
        BreadthFirstSearch();
    }
    
    for(int i = 1; i <= n; i++) {
        cout<<i<<' '<<list[i]<<endl;
    }
    
    return 0;
}

void BreadthFirstSearch() {
    int u = q.pop();
    int i = 0;
    
    while(a[u][i] != 0) {
        if(list[ a[u][i] ] != -1) {
            i++;
            continue;
        }
        q.push(a[u][i]);
        list[ a[u][i] ] = list[u] + 1;
        i++;
    }
}

C++: #include <iostream>
#include <queue>
using namespace std;

int G[110][110];
int N,num,lines,tar;
int D[1000];
int ret[1000];

void bfs(int src);

int main() {
	for (int i=0;i<110;i++) {
		for(int j=0;j<110;j++) {
			G[i][j] = 0;
		}
	}
	for(int i=0;i<1000;i++) {
		D[i] = 100;
	}
	cin >> N;
	for(int i=0;i<N;i++) {
		cin >> num >> lines;
		for(int j=0;j<lines;j++) {
			cin >> tar;
			G[num][tar] = 1;
		}
	}
	/*
	for(int i=0;i<num+1;i++) {
		for(int j=0;j<num+1;j++) {
			cout << G[i][j] << " ";
		}
		cout << endl;
	}
	*/
	bfs(1);
	for(int i=1;i<num+1;i++) {
		if(ret[i] == 100) ret[i] = -1;
		cout << i << " " << ret[i] << endl;
	}

}
void bfs(int src) {
	int cur,dst;
	//cerr << "bfs root = " << src << endl;
	queue<int> Q; // ??´??°????????????????????\????????????
	Q.push(src);
	D[src] = 0; // ?????????
	while (! Q.empty()) {
		int cur = Q.front(); // ??????????´????????????????
		Q.pop();
// ???????¢??????¨??¨???
		ret[cur] = D[cur];
		//cerr << cur << " " << D[cur] << endl;
		for (int dst=1;dst<N+1;dst++) { // ???????????? dst ????????????
			if ((G[cur][dst] == 1) && (D[dst] == 100)) { // cur ?????? dst ??????????????????dst ???????¨??????????
				D[dst] = D[cur]+1; //
				Q.push(dst); // dst ????¨???????????????????
			}
		}
	}
}

C++: #include<queue>
#include<iostream>
using namespace std;
int D[110];
int N,n,u,k,v;
int main()
{
    queue<int>Q;
    cin>>N;
    int A[N][N];
    for(int p=0;p<N;p++)
        for(int q=0;q<N;q++)
            A[p][q]=0;
    for(int p=0;p<N;p++)
        D[p]=2147483647;
    for(n=0;n<N;n++)
    {
        cin>>u>>k;
        for(int i=0;i<k;i++)
        {
            cin>>v;
            A[u-1][v-1]=1;
        }
    }
    Q.push(1);
    D[0]=0;
    while(!Q.empty())
    {
        int cur=Q.front();
        Q.pop();
            for(int j=0;j<N;j++)
            {
                if(A[cur-1][j]==1&&D[j]==2147483647)
                {
                    Q.push(j+1);
                    D[j]=D[cur-1]+1;
                }
            }
    }
    for(int i=0;i<N;i++)
    {
        if(D[i]==2147483647)
            D[i]=-1;
    }
    for(int i=0;i<N;i++)
    {
        cout<<(i+1)<<' '<<D[i]<<endl;
    }
    return 0;
}