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 <vector>
#include <queue>

using namespace std;

queue<int> que;

void bfs(vector<vector<int> > &G, int s, int *seen) {
    seen[s] = 0;
    que.push(s);
    
    while (!que.empty()) {
        int u = que.front();
        que.pop();

        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i];

            if (seen[v] != -1)  
                continue;

            seen[v] = seen[u] + 1;
            que.push(v);
        }
    }
}

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

    int *seen = new int[n];
    for (int i = 0; i < n; i++)
        seen[i] = -1;
    seen[0] = 0;

    bfs(G, 0, seen);

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

    return 0;
}

C++: #include<cstdio>
#include<queue>
using namespace std;
static const int INF=(1<<21);
static const int MAXN=101;
struct p{
	int d;
	int cnt;
};
int main()
{
	int n,m[MAXN][MAXN],i,j,u,k,v,l,t[MAXN];
	p e,f;
	queue<p> q;
	bool used[MAXN]={};
	scanf("%d",&n);
	for(i=1;i<n+1;i++){
		for(j=1;j<n+1;j++){
			m[i][j]=INF;
		}
	}
	for(i=0;i<n;i++){
		scanf("%d %d",&u,&k);
		for(j=0;j<k;j++){
			scanf("%d",&v);
			m[u][v]=1;
		}
	}
	used[1]=true;
	e.d=1;
	e.cnt=0;
	q.push(e);
	while(!q.empty()){
		e=q.front();
		q.pop();
		for(i=1;i<n+1;i++){
			if(m[e.d][i]==1&&used[i]==false){
				f.d=i;
				f.cnt=e.cnt+1;
				q.push(f);
				used[i]=true;
				t[i]=e.cnt+1;
			}
		}
	}
	for(i=1;i<n+1;i++){
		if(used[i]==true){
			printf("%d %d\n",i,t[i]);
		}
		else{
			printf("%d -1\n",i);
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  queue <int> q1;
  int n,i,j;
  cin >> n;
  int d[n][n];
  int m[n];
  int v[n];
  rep(i,n){
    m[i]=10000;
    v[i]=0;
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
        d[i][j]=10000;
      }
    }
  }
  m[0]=0;

  rep(i,n){
    int u,k;
    cin >> u >> k;
    if(k>0){
      rep(j,k){
        int v;
        cin >> v;
          d[u-1][v-1]=1;
      }
    }
  }   

  q1.push(0);
  while(!q1.empty()){
    int u = q1.front(); q1.pop();
      rep(j,n){
        if(d[u][j]==1 && v[j]==0){
          q1.push(j);
          m[j] = min(m[j],m[u]+1);
        }
      }
    v[u]=1;
  }

  rep(i,n){
    cout << i+1 << ' ';
    if(m[i]==10000){
      cout << -1 << endl;
    }else{
      cout << m[i] << endl;
    }
  }

  return 0;
}

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

using namespace std;

#define MAX 100
#define INF 1e9

int n;
int G[MAX][MAX];
int dist[MAX];

void bfs(){
  bool used[MAX];
  fill(dist,dist+n,INF);
  fill(used,used+n,false);
  dist[0] = 0;

  queue<int> Q;
  Q.push(0);

  while(!Q.empty()){
    int v = Q.front(); Q.pop();

    for(int i = 0 ; i < n ; i++){
      if(!used[i] && G[v][i] != INF){
	used[i] = true;
	dist[i] = dist[v] + 1;
	Q.push(i);
      }
    }
  }
}

int main(){
  cin >> n;

  fill(G[0],G[MAX],INF);
  for(int i = 0 ; i < n ; i++){
    int u,k,v;
    cin >> u >> k;
    for(int j = 0 ; j < k ; j++){
      cin >> v;
      G[u-1][v-1] = 1;
    }
  }

  bfs();

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

  return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const double pi = 3.141592653589793;
#define MOD 1000000007
#define INF 10000000000000


signed main(){
  int u,k,id;
  cin>>u;
  vector<vector<int> > v(u,vector<int>(0));
  rep(i,u){
    cin>>id>>k;
      rep(j,k){
        int t;cin>>t;
        v[id-1].push_back(t);
  }
}
queue<int>q;
q.push(1);
vector<int>ans(u);
rep(i,u){ans[i]=-1;}
ans[0]=0;
int f;
while(!q.empty()){
  f=q.front();
  q.pop();
  rep(i,v[f-1].size()){
    if(ans[v[f-1][i]-1]<0){
    ans[v[f-1][i]-1]=ans[f-1]+1;
    q.push(v[f-1][i]);
  }
  }
}
  rep(i,u){
    cout<<i+1<<' '<<ans[i]<<endl;
  }
}

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

int n;
vector <int> G[101];
queue <int> Q;
int  d[100];
int  dis[101];


int visit(int x){
  int t,i;
  
  Q.push(x);
  dis[x] = 0;
  d[x] = 1;

  while(Q.empty() == false) {
    
    // cout << "start Q" <<endl;

    t = Q.front();
    Q.pop();

    for(i=0; i<G[t].size();i++) {
      if(d[G[t][i]] == 0 ) {
	d[G[t][i]] = 1;
	dis[G[t][i]] = dis[t] + 1;

	Q.push(G[t][i]);
      }

    }
  }
}
    
int main() {
  
  int i,j,k,l,u,a,t;
  
  
  scanf("%d",&n);

  
  
  for(i=0;i<n;i++){
    d[i] = 0;
    dis[i] = -1;
    scanf("%d %d ",&k,&u);
    
    for(j=0;j<u;j++) {
      scanf("%d",&a);
      G[k-1].push_back(a-1);
      // printf("G[%d][%d] = 1\n",i,k-1);
    }
  }
  
  
  visit(0);

  

  for(i=0;i<n;i++) cout << i+1<<" " << dis[i] <<endl;



  return 0;

    
    
}

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

using namespace std;

const int maxn=1e2+5;
const int inf=(1<<21);

int n,m[maxn][maxn];
int d[maxn];

void bfs(int s)
{
    queue<int>q;
    q.push(s);
    for(int i=0;i<n;i++){
        d[i]=inf;
    }
    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]!=inf){
                continue;
            }
            d[v]=d[u]+1;
            q.push(v);
        }
    }
    for(int i=0;i<n;i++){
        cout<<i+1<<" "<<((d[i]==inf)?(-1):d[i])<<endl;
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            m[i][j]=0;
        }
    }
    int u,k,v;
    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;
}
/*
4
1 2 2 4
2 1 4
3 0
4 1 3
*/

C++: 
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <ctime>
#include <string>
#include <queue>

using namespace std;

const int MAX_N = 100;
int n;
int suji[MAX_N];
int	num[MAX_N];
int adj[MAX_N][MAX_N];
int ans[MAX_N][MAX_N];
int D[MAX_N];

void bfs(int src){
	queue<int> Q;
	Q.push(src);
	D[src] = 0;

	while(!Q.empty()){
		int cur = Q.front();
		Q.pop();

		for(int i = 1; i < n; i++){
			if(ans[cur][i] == 1 && D[i] == -1){
				D[i] = D[cur] + 1;
				Q.push(i);
			}
		}
	}
}


int main(void){

	cin >> n;
	for(int i = 0; i < n; i++){
		
		cin >> suji[i] >> num[i];
		D[i] = -1;
		
		for(int j = 0; j < num[i]; j++){
			cin >> adj[i][j];
			adj[i][j] -= 1;
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			ans[i][j] = 0;
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < num[i]; j++){
			ans[i][adj[i][j]] = 1;
		}
	}

	bfs(0);

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