Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: #include <bits/stdc++.h>
 
using namespace std;

int main() {
    int n; cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n; i++) {
        int u, k; cin >> u >> k; u--;
        while (k--) {
            int v; cin >> v; v--;
            g[u].push_back(v);
        }
        sort(g[u].begin(), g[u].end());
    }
    vector<int> d(n, -1);
    d[0] = 0;
    queue<int> q; q.push(0);
    while (q.size()) {
        int v = q.front(); q.pop();
        for (auto x : g[v]) {
            if (d[x] < 0) {
                d[x] = d[v] + 1;
                q.push(x);
            }
        }
    }
	for (int i = 0; i < n; i++) cout << i + 1 << " " << d[i] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;

struct Graph {
    enum Color {WHITE, GRAY, BLACK};
    vector<vector<int>> adj;
    vector<Color> color;
    vector<int> dist;
    Graph(int n) {
        adj.assign(n, vector<int>());
        color.assign(n, WHITE);
        dist.assign(n, -1);
    }
};

int main()
{
    int n;
    cin >> n;
    Graph g(n);
    for (int i = 0; i < n; i++) {
        int id, k;
        cin >> id >> k;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            g.adj[id - 1].push_back(v - 1);
        }
    }

    queue<int> q;
    g.color[0] = Graph::GRAY;
    g.dist[0] = 0;
    q.push(0);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (auto v : g.adj[u]) {
            if (g.color[v] == Graph::WHITE) {
                g.color[v] = Graph::GRAY;
                g.dist[v] = g.dist[u] + 1;
                q.push(v);
            }
        }
        g.color[u] = Graph::BLACK;
    }

    for (int i = 0; i < n; i++) {
        cout << (i + 1) << " " << g.dist[i] << endl;
    }
}

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

#define MAX_NODE 10000
#define INF 1000000000

using namespace std;

int d[MAX_NODE];
vector<int> adj[MAX_NODE];

int bfs(int n, int start){
	queue<int> que;
	for(int i=0;i<n;i++){
		d[i]=(i==start)?0:-1;
	}
	que.push(start);
	while(que.empty()==0){
		int from=que.front();
		que.pop();
		for(int i=0;i<adj[from].size();i++){
			int to=adj[from][i];
			if(d[to]==-1){
				d[to]=d[from]+1;
				que.push(to);
			}
		}
	}
}	

int main(){
	int n,u,k,v;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>u>>k;
		for(int j=0;j<k;j++){
			cin>>v;
			v--;
			adj[u-1].push_back(v);
		}
	}
	bfs(n,0);
	for(int i=0;i<n;i++){
		cout<<i+1<<" "<<d[i]<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define N 1000
bool M[N][N];

int main(){
    int n,u,k,v;
    queue<int> Q;

    cin >> n;
    for(int i=0;i<n;i++){
        cin >> u >> k;
        u--;
        for(int j=0;j<k;j++){
            cin >> v;
            v--;
            M[u][v]=true;
        }
    }

    int d[N]={0};
    Q.push(0);
    while(!Q.empty()){
        u=Q.front();
        Q.pop();
        for(int i=0;i<n;i++){
            if(M[u][i] && d[i]==0){
                d[i]=d[u]+1;
                Q.push(i);
            }
        }
    }

    for(int i=0;i<n;i++){
        printf("%d %d\n",i+1,d[i]);
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define MAX 100
using namespace std;

int n;
vector<int> G[MAX];
int d[MAX];

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		d[i] = -1;
	}
	for (int i = 0, u, k; i < n; i++) {
		cin >> u >> k;
		for (int j = 0, v; j < k; j++) {
			cin >> v;
			G[u - 1].push_back(v - 1);
		}
	}
	queue<int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		for (int &v : G[q.front()]) {
			if (d[v] == -1) {
				q.push(v);
				d[v] = d[q.front()] + 1;
			}
		}
		q.pop();
	}

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

C++: #include<bits/stdc++.h>

using namespace std;
using ll = long long;

int n;
vector<vector<int>> hen;
vector<bool> seen;
vector<int> dist;

void bfs()
{
    queue<int> que;
    que.push(0);
    dist[0] = 0;
    seen[0] = true;
    while(que.size())
    {
        auto cur = que.front();
        que.pop();
        for(auto i : hen[cur]){
            if(seen[i])continue;
            seen[i] = true;
            dist[i] = dist[cur] + 1;
            que.push(i);
        }
    }
}




int main()
{
    cin >> n;
    hen.resize(n);
    dist.resize(n, -1);
    seen.resize(n);
    for(int i = 0; i < n; i++){
        int u, k; cin >> u >> k; u--;
        for(int j = 0; j < k; j++){
            int tmp; cin >> tmp; tmp--;
            hen[u].push_back(tmp);
        }
    }
    bfs();

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


}

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

typedef struct{
  int A[1000];
  int ans;
  bool c;
}node;
node T[1000];
int Q[10000000];
int head=0;
int tail=1;
int n;

void function(int num){
  while(true){
    if(head==tail)break;
    num=Q[head++];
    for(int i=2;i<=n;i++){
      if(T[num].A[i]==1){
	if(T[i].ans==0){
	  T[i].ans=T[num].ans+1;
	  T[num].A[i]=0;
	}
	Q[tail++]=i;
      }
    }
    //    cout<<"Q "<<Q[head]<<endl;
    //  cout<<head<<" "<<tail<<endl;
  }

}

int main(){
  int num;
  int son;
  int z;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>num;
    cin>>son;
    for(int j=0;j<son;j++){
      cin>>z;
      T[num].A[z]=1;
      }
  }
  Q[0]=1;
  function(1);
  T[1].ans=0;
  for(int i=2;i<=n;i++){
    if(T[i].ans==0){
      T[i].ans=-1;
    }
  }

  for(int i=1;i<=n;i++){
      cout<<i<<" "<<T[i].ans<<endl;
  }


  return 0;
}

C++: #include<bits/stdc++.h>

struct node{
    int d, flag;
    node(){
        d = 0;
        flag = 0;
    }
};

int main(void){
    int n;
    int ind, num, a;
    int time, ndi;

    std::cin >> n;
    std::vector<node> nd(n);
    std::vector<std::vector<int>> b(n, std::vector<int>(n));
    std::queue<int> st;

    for(int i = 0; i < n; i++){
        std::cin >> ind >> num;
        ind--;
        for(int i = 0; i < num; i++){
            std::cin >> a;
            a--;
            b[ind][a] = 1;
        }
    }

    time = 0;
    int x = 0;
    //for(int x = 0; x < n; x++){
        if(nd[x].flag == 0){
            ndi = x;
            time++;
            nd[x].flag = 1;
        
            st.push(x);
        }
    
        

        while(st.empty() != 1){
            //std::cout << ndi+1 << " " << time << std::endl;
            //time++;
            int tmp = -1;
            int tmp2;
            for(int i = 0; i < n; i++){
                if(b[ndi][i] == 1 && nd[i].flag == 0){
                    tmp = i;
                    tmp2 = x;
                    break;
                }
            }
            if(tmp == -1){
                //std::cout << ndi+1 << " " << time << std::endl;

                nd[ndi].flag = 1;
                st.pop();
                if(st.empty() != 1){
                    ndi = st.front();
                }

                continue;
            }

            nd[tmp].flag = 1;
            nd[tmp].d = nd[ndi].d + 1;
            st.push(tmp);
            
        }
    //}
    for(int i = 0; i < n; i++){
        if(nd[i].flag == 0 && i != 0){
            nd[i].d = -1;
        }
    }
    for(int i = 0; i < n; i++){
        std::cout << i+1 << " " << nd[i].d << std::endl;
    }

    return 0;
}