Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

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

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;

int G[100][100];
int d[100];

void init_d(int n){
    rep(i,n){
        d[i]=-1;
    }
}
void input_graph(int n){
    int u,k,v;
    rep(i,n){
        scanf("%d %d",&u,&k);
        u--;
        rep(j,k){
            scanf("%d",&v);
            v--;
            G[u][v] = 1;
        }
    }
}
void bfs(int s,int n){    
    queue<int> que;
    que.push(s);
    while(que.size()>0){
        int u = que.front();
        que.pop();
        rep(i,n){
            if(G[u][i]==1 && d[i] == -1){
                d[i]=d[u]+1;
                que.push(i);
            }
        }
    }
}
void print_graph(int n){
    rep(i,n){
            printf("%d %d\n",i+1,d[i]);
    }
}

int main(){
    // 制約見て！！！デカかったらlong longにすること！
    int n;
    cin >> n;
    init_d(n);
    input_graph(n);
    d[0]=0;
    bfs(0,n);
    print_graph(n);
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 100

int n, M[MAX][MAX] = {};
int d[MAX];

void bfs(int s) {
	queue<int> q;
	q.push(s);
	d[s] = 0;
	for (int u; !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 /*zefo-based*/ << " " << ((d[i] == INF) ? (-1) : d[i]) << endl;
	}
}

int main() {
	cin >> n;
	memset(d, INF, sizeof(d));
	for (int i = 0, u, k; i < n; i++) {
		cin >> u >> k;
		u--;
		for (int j = 0, v; j < k; j++) {
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}

	bfs(0); //zero-based

	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 main() {
	int a[101][101] = {{0}};
	int u, k, v, km;
	km = 0;
	while ( cin >> u, k ) {
		for ( int i = 1; i <= k; i++ ) {
			cin >> v;
			a[u][v] = 1;
		}
		km++;
	}
	int n0;
	int b[101] = {- 1};
	queue<int> q;
	q.push( 1 );
	b[1] = 0;
	int cou = 0;
	for ( int i = 1; i <= km; i++ ) {
		n0 = q.front();
		q.pop();
		cou++;
		for ( int j = 1; j <= km + 1; j++ ) {
			if ( a[n0][j] == 1 && b[j] == - 1 ) {
				q.push( j );
				b[j] = cou;
			}
			if ( q.empty() ) break;
		}
	}
	for ( int i = 1; i <= km; i++ )
		cout << i << " " << b[i] << endl;
}

C++: #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2



int d[MAX];
vector<int> V[MAX];
int tt=0;
queue<int> Q;

int main()  {
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    d[i]=-1;
  }

  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--;
      V[u].push_back(v);
    }
  }

  Q.push(0);
  d[0]=tt;

  while(!Q.empty()){
    v=Q.front(); Q.pop();
    for(int i=0;i<V[v].size();i++){
      if(d[V[v][i]]==-1) {
        Q.push(V[v][i]);
        d[V[v][i]]=d[v]+1;

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

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

using namespace std;

int n, M[100][100], d[100];

void bfs (int s) {

    int i, u, v;

    queue<int> Q;
    Q.push(s);
    for (i = 0; i < n; i++) {
        d[i] = (1 << 21);
    }
    d[s] = 0;
    while (!q.empty()) {
        u = Q.front();
        Q.pop();
        for (v = 0; v < n; v++) {
            if (M[u][v] == 0) {
                continue;
            }
            if (d[v] != (1 << 21)) {
                continue;
            }
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for (i = 0; i < n; i++) {
        cout << i + 1 << " " << ((d[i] == (1 << 21)) ? (-1) : d[i]) << endl;
    }
}

int main () {

    int u, k, v, i, j;

    cin >> n;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            M[i][j] = 0;
        }
    }

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

    bfs (0);

    return 0;

}

C++: #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

bool A[110][110],flag[110];
int d[110];
int n;

void bfs(){
	queue<int> Q;
	Q.push(1);
	d[1]=0;
	while(Q.empty()==0){
		int u=Q.front();
		Q.pop();
		for(int i=2;i<=n;i++){
			if(A[u][i]==true && flag[i]==false ){
				flag[i]=true;
				Q.push(i);
				d[i]=d[u]+1;
			}
		}
	}
	for(int i=2;i<=n;i++) if(flag[i]==false) d[i]=-1;
	return ;
}

int main(){
	int a,b,c;
	cin>>n;
	for(int i=1;i<=n;i++){
		flag[i]=false;
			for(int j=1;j<=n;j++){
				A[i][j]=false;
		}
	}
	for(int i=1;i<=n;i++){
		cin>>a>>b;
		for(int j=1;j<=b;j++){
			cin>>c;
			A[a][c]=true;
		}
	}
	bfs();
	for(int i=1;i<=n;i++) cout<<i<<" "<<d[i]<<endl;	
	return 0;
}