Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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

using namespace std;
struct node
{
    node() {};
    node(int i, int n, int* xs) : id {i}, ns {vector<int>(xs, xs+n)}, d {-1} {};
    int id;
    vector<int> ns;
    int d;
};

void bfs(node* xs, int n, int st)
{
    queue<int> que {};
    bool used[n+1] {false};
    que.push(st);
    xs[st].d = 0;
    used[st] = true;
    while(que.size())
    {
        int p = que.front();
        que.pop();
        vector<int> ns = xs[p].ns;
        for(auto x : ns)
        {
            if(used[x])
            {
                continue;
            }
            xs[x].d = xs[p].d + 1;
            used[x] = true;
            que.push(x);
        }
    }
}

int main()
{
    int n;
    cin >> n;
    node graph[n+1];
    for(int i = 0; i < n; ++i)
    {
        int id;
        int m;
        cin >> id >> m;
        int xs[m];
        for(int j = 0; j < m; ++j)
        {
            cin >> xs[j];
        }
        graph[id] = node(id, m, xs);
    }
    bfs(graph, n, 1);
    for(int i = 1; i <= n; ++i)
    {
        cout << i << ' ' << graph[i].d << endl;
    }
}

C++: #include <stdio.h>
#include <string.h>
#include<iostream>
#include<queue>
using namespace std;

char s[13][1];
int D[102][102];
int A[103],f[103],w,h,n,sx,sy,gx,gy,e,g[103];
int   kensa(int ,int);
int main(){
	queue<int> P;
	int i,j,a,b,c,d;
	for(i=0;i<100;i++){
		for(j=0;j<100;j++){
			D[i][j]=-1;		
		}					
	}						
	for(i=0;i<100;i++){		
		A[i]=0;				
		f[i]=-1;
		g[i]=0;
	}							
	scanf("%d" ,&a);//     a=4
	for(i=1;i<=a;i++){
        scanf("%d",&b);		//
		scanf("%d",&b);			//b=2					//
		for(j=1;j<=b;j++){		//
		scanf("%d",&c);			//					//
		D[i][c]=1;
//
		}						//D[???][???]
	}				
	for(i=1;i<=a;i++){
		for(j=1;j<=a;j++){
		//	printf("%d %d %d\n",i,j,D[i][j]);
		}	
	}//
	P.push(1);
	//P???1
	f[1]=0;
	while(!P.empty()){			//
		d=P.front();
		//
		A[d]=1;
		//
		P.pop();				//
		for(i=1;i<=a;i++){
			//printf("%d %d %d\n",d,i,D[d][i]);//
			if(D[d][i]==-1){		//
			continue;			//
			}					//
			if(A[i]==1){		//
			continue;			//
			}					//
			P.push(i);			//
			f[i]=g[d]+1;
			g[i]=g[d]+1;
			A[i]=1;
			//printf("%d\n",P.back());
		}	
	}							//
	for(i=1;i<=a;i++){			//
	printf("%d %d\n",i, f[i]);	//
	}
	return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include<iostream>
#include<queue>
using namespace std;

char s[13][1];
int D[102][102];
int A[100],f[100],w,h,n,sx,sy,gx,gy,e,g[100];
int   kensa(int ,int);
int main(){
	queue<int> P;
	int i,j,a,b,c,d;
	for(i=0;i<100;i++){
		for(j=0;j<100;j++){
			D[i][j]=-1;		
		}					
	}						
	for(i=0;i<100;i++){		
		A[i]=0;				
		f[i]=0;
		g[i]=0;
	}							
	scanf("%d" ,&a);//     a=4
	for(i=1;i<=a;i++){
        scanf("%d",&b);		//
		scanf("%d",&b);			//b=2					//
		for(j=1;j<=b;j++){		//
		scanf("%d",&c);			//					//
		D[i][c]=1;
//
		}						//D[???][???]
	}				
	for(i=1;i<=a;i++){
		for(j=1;j<=a;j++){
		//	printf("%d %d %d\n",i,j,D[i][j]);
		}	
	}//
	P.push(1);
	//P???1
	while(!P.empty()){			//
		d=P.front();
		//
		A[d]=1;
		//
		P.pop();				//
		for(i=1;i<=a;i++){
			//printf("%d %d %d\n",d,i,D[d][i]);//
			if(D[d][i]==-1){		//
			continue;			//
			}					//
			if(A[i]==1){		//
			continue;			//
			}					//
			P.push(i);			//
			f[i]=g[d]+1;
			g[i]=g[d]+1;
			A[i]=1;
			//printf("%d\n",P.back());
		}	
	}							//
	for(i=1;i<=a;i++){			//
	printf("%d %d\n",i, f[i]);	//
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

const int MAX_V = 101;
vector<int> adj[MAX_V];
bool used[MAX_V];
int id[MAX_V];

void bfs(int s){
  queue<int> que;
  que.push(s);
  used[s]=true;
  id[s]=0;
  for(;!que.empty();){
    int p=que.front();
    que.pop();
    for(int i:adj[p]){
      if(!used[i]){
	que.push(i);
	used[i]=true;
	id[i]=id[p]+1;
      }
    }
  }
}

int main(){
  int n;cin>>n;
  REP(i,n){
    int u,k,v;cin>>u>>k;
    REP(j,k){
      cin>>v;
      adj[u].push_back(v);
    }
  }
  bfs(1);
  REP(i,n) cout<<i+1<<" "<<id[i+1]<<endl;
}

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

struct node {
	std::vector<int> child;
	int distance;
};

int main() {
	int node_num;

	std::cin >> node_num;

	std::vector<node> n(node_num);

	for (int i = 0; i < node_num; i++) {
		int num, to, node_val;
		std::cin >> node_val >> num;
		for (int j = 0; j < num; j++) {
			std::cin >> to;
			n[node_val - 1].child.push_back(to);
		}
		n[i].distance = -1;
	}

	//???????????????????????¢0
	n[0].distance = 0;
	std::queue<int> q;
	int dist = 1;
	q.push(1);
	while(q.size() != 0){
		int num = q.front();
		q.pop();
		for (auto it = n[num - 1].child.begin(); it != n[num - 1].child.end(); it++) {
			q.push(*it);
			if (n[*it - 1].distance > dist || n[*it - 1].distance == -1) {
				n[*it - 1].distance = dist;
			}
		}
		dist++;
	}

	for (int i = 0; i < node_num; i++) {
		std::cout << i + 1 << " " << n[i].distance << std::endl;
	}
	return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

int n ,dist[128];
bool rel[128][128];

int main() {
	scanf("%d", &n);
	
	for(int i = 1; i <= n; i++){
		int u, k;
		scanf("%d %d", &u, &k);
		
		dist[u] = INT_MAX;
		for(int j = 1; j <= n; j++){
			rel[u][j] = false;
		}
		while(k--){
			int v;
			scanf("%d", &v);
			rel[u][v] = true;
		}
	}
	
	queue<int> visit;
	visit.push(1);
	dist[1] = 0;
	
	while(visit.size()){
		for(int i = 1; i <= n; i++){
			if(rel[visit.front()][i] && 1 + dist[visit.front()] < dist[i]){
				visit.push(i);
				dist[i] = 1 + dist[visit.front()];
			}
		}
		visit.pop();
	}
	
	for(int i = 1; i <= n; i++){
		printf("%d %d\n", i, dist[i]);
	}
	return 0;
}