Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

func (q *Queue) push(v Pair) {
	*q = append(*q, v)
}

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

	for len(queue) > 0 {
		p := queue.pop()
		d := p.d + 1
		for _, v := range graph[p.v] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(Pair{v, d})
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <math.h>
using namespace std;
typedef long long int ll;
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int n;
int graph[101][101];
int d[101];
int k[101];
bool beg[101];
int cnt=0;
int pre=-6;
queue<int>now;
queue<int>nextg;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        beg[i]=false;
        d[i]=-1;
        int number;
        cin>>number;
        cin>>k[i];
        for(int j=1;j<=k[i];j++)
        {  cin>>graph[i][j];
        }
    }
    for(int j=1;j<=k[1];j++)
    {  now.push(1);
        nextg.push(graph[1][j]);
    }
    d[1]=0;
    beg[1]=true;
    while(now.size())
    {
        int nowt=now.front();
        int nextt=nextg.front();
        
        now.pop();
        nextg.pop();
        if(beg[nextt]==false)
        {
            d[nextt]=d[nowt]+1;
            beg[nextt]=true;
            pre=nowt;
            for(int j=1;j<=k[nextt];j++)
            {
                now.push(nextt);
                nextg.push(graph[nextt][j]);
                //cout<<"now "<<nextt<<"nextg "<<graph[nextt][j]<<endl;
            }
        }
        
    }
    for(int i=1;i<=n;i++)
    {
        if(d[i]<0)
            d[i]=-1;
        cout<<i<<" "<<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];
int 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[u] != 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>
#include<queue>

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

int n,M[N][N];
int 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>
#include<queue>
using namespace std;
int N;
int D[101],d[101][101];
void bfs(int src){
    int u,k,a;
    bool visit[101];
    for(int i=1;i<=N;++i){
        cin>>u>>k;
        for(int j=0;j<k;++j){
            cin>>a;
            d[u][a]=1;
        }
        visit[i]=true;
    }
    queue<int>Q;
    Q.push(src);
    D[src]=0;
    visit[src]=false;
    while(!Q.empty()){
        int cur=Q.front();
        Q.pop();
        for(int i=1;i<=N;++i){
            if(d[cur][i]==1&&visit[i]){
                visit[cur]=false;
                visit[i]=false;
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
    for(int i=1;i<=N;++i){
        if(i!=1&&D[i]==0){
            D[i]=-1;
        }
        cout<<i<<" "<<D[i]<<endl;
    }
}
int main(){
    cin>>N;
    int start=1;
    bfs(start);
}

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;
}