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<queue>
using namespace std;
int m[105][105],n,v[105],d[105];
void bfs(int s)
{
    v[s]=true;
    d[s]=0;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(int i=0;i<n;i++)
        {
             if(m[u][i]&&!v[i])
            {
                d[i]=d[u]+1;
                v[i]=true;
                q.push(i);
            }
        }
    }
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)d[i]=-1;
    for(int i=0;i<n;i++)
    {
        int u,v,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            cin>>v;
            m[u-1][v-1]=true;
        }
    }
    bfs(0);
    for(int i=0;i<n;i++)cout<<i+1<<' '<<d[i]<<endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

vector<vector<int>> G;

// O(HW)
int n;
int d[110];
void bfs(){
    queue<int> que;
    
    que.push(0); // (0,0)スタート
    d[0]=0;
    while(!que.empty()){
        int p=que.front();
        que.pop();
        for(int nv : G[p]){
            if(d[nv]!=-1) continue;
            que.push(nv);
            d[nv]=d[p]+1;
        }
    }
}

int main() {
    
    cin>>n;
    G.resize(n);
    rep(i,n){
        int u,k;
        cin>>u>>k;
        u--;
        rep(j,k){
            int v;
            cin>>v;
            v--;
            G[u].push_back(v);
        }
    }

    rep(i,n) d[i]=-1;
    bfs();

    rep(i,n){
        cout<<i+1<<" "<<d[i]<<endl;
    }

}

C++: #include<bits/stdc++.h>
using namespace std;
int i,j,n,u,k,v,g[101][101],d[101],f[101];
int main(void){
  for(i=1;i<101;i++)d[i]=1<<30;
  d[1]=0;
  cin>>n;
  for(i=0;i<n;i++){
    cin>>u>>k;
    for(j=0;j<k;j++){
      cin>>v;
      g[u][v]++;
    }
  }
  queue<int>q;
  q.push(1);
  while(!q.empty()){
    u=q.front();q.pop();
    f[u]=1;
    for(i=1;i<101;i++){
      if(g[u][i]&&!f[i]){
        d[i]=min(d[i],d[u]+1);
        q.push(i);
      }
    }
  }
  for(i=1;i<=n;i++)cout<<i<<" "<<(d[i]==1<<30?-1:d[i])<<endl;
  return 0;
}

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

using namespace std;
const int N = 100;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n;
vector<vector<int>> G(N,vector<int>(N,0));
vector<int> d(N,-1);
vector<int> colors(N,WHITE);

void bfs(){
    queue<int> q;
    q.push(0);
    colors[0] = GRAY;
    d[0] = 0;
    while(!q.empty()){
        int u = q.front();q.pop();
        for(int i = 0;i<n;i++){
            if(colors[i]==WHITE && G[u][i]){
                q.push(i);
                colors[i] = GRAY;
                d[i] = d[u] + 1;
            }
        }
        colors[u] = BLACK;
    }
}

int main()
{
    //freopen("data.txt","r",stdin);
    cin >> n;
    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--;
            G[u][v] = 1;
        }
    }
    bfs();
    for(int i =0;i<n;i++){
        printf("%d %d\n",i+1,d[i]);
    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define N 105 // number of nodes
vector<int> neighbors[N]; //start from 1

//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C bfs
int dist[N];
void bfs(int start, int key,int d){
	if(start == key){
		//if(d < dist[key])
			dist[key] = d;
		return;
	} 
	for(int i = 0; i< neighbors[start].size(); i++){
		bfs(neighbors[start].at(i), key,d+1);
	}
}

int main()
{
    int n;
    scanf("%d",&n);
    memset(dist, -1, sizeof(dist));
    for(int i = 0; i<n; i++){
        int node;
        scanf("%d",&node);
        int num;
        scanf("%d",&num);
        for(int j = 0; j<num; j++){
        	int temp;
        	scanf("%d",&temp);
        	neighbors[node].push_back(temp);
		}
    }
    for(int i = 1; i<=n; i++){
    	bfs(1,i,0);
	}
    for(int i = 1; i<=n; i++){
    	printf("%d %d\n",i, dist[i]);
	}
    return 0;
}

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

int z[100],n;
int a[100][100];

void bfs(){
  int m;
  z[0]=0;
  queue<int> q;
  q.push(0);
  while(!q.empty()){
    m=q.front();
    q.pop();
    for(int i=0;i<n;i++){
      if(a[m][i]==1){
	q.push(i);
	z[i]=min(z[i],z[m]+1);
      }
    }
  }
}


int main(){
  int p,x;
  for(int i=0;i<100;i++){
    z[i]=INF;
    for(int j=0;j<100;j++) a[i][j]=0;
  }
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>p>>p;
    for(int j=0;j<p;j++){
      cin>>x;
      a[i][x-1]=1;
    }
  }
  bfs();
  for(int i=0;i<n;i++){
    if(z[i]!=INF) cout<<i+1<<" "<<z[i]<<endl;
    else cout<<i+1<<" "<<-1<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int INF=(1<<21);
int n,m[101][101];
int d[101];
void bfs(int s){
  queue<int>q;
  q.push(s);
  for(int i=0;i<n;i++){
    d[i]=INF;
  }
  d[s]=0;
  while(!q.empty()){
    int 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(){
  int u,k,v;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    for(int j=0;j<k;j++){
      cin>>v;
      m[u-1][v-1]=1;
    }
  }
  bfs(0);
  return(0);
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int G[101][101];
int d[101];

void explore(int id, int n,int cnt)
{
	if (d[id] == -1)
	{
		d[id] = cnt;
	}
	else
	{
		d[id] = min(d[id], cnt);
	}

	for (int i = 1; i <= n; i++)
	{
		if (G[id][i] == 1)
		{
			explore(i, n, cnt + 1);
		}
	}
}

int main()
{
	int n;
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		int id, k;
		cin >> id >> k;

		for (int i = 0; i < k; i++)
		{
			int v;
			cin >> v;
			G[id][v] = 1;
		}
	}

	for (int i = 1; i <= 100; i++)
	{
		d[i] = -1;
	}

	explore(1, n, 0);

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

	return 0;
}