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>
using namespace std;
typedef long long ll;
#define loop(i,a,b) for(i=a;i<b;i++)
#define rloop(i,a,b) for(i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
const int inf=1000000001;
const ll INF=1e16;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;
static const int N=101;

int n;
vi d(N,-1),color(N,WHITE);
vii M(N,vi(N,0));

void bfs();
void bfs_visit(int);

int main(){
	int i,j;
	cin>>n;
	int u,k,v;
	loop(i,0,n){
		cin>>u>>k;
		loop(j,0,k){
			cin>>v;
			M[u][v]=1;
		}
	}
	
	bfs();
}

void bfs(){
	int u;
	bfs_visit(1);
	loop(u,1,n+1){
		cout<<u<<" "<<d[u]<<endl;
	}
}

void bfs_visit(int r){
	queue<int> q;
	int v;
	q.push(r);
	d[r]=0;
	while(!q.empty()){
		int u=q.front();
		loop(v,1,n+1){
			if(M[u][v] && color[v]==WHITE){
				q.push(v);
				color[v]=GRAY;
				d[v]=d[u]+1;
			}
		}
		color[u]=BLACK;
		q.pop();
	}
}

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

using namespace std;

#define MAX 101

int d[MAX],visited[MAX];
vector<int> G[MAX];
queue<int> Q;

int main(){
  int n; cin >> n;
  for(int i=0;i<n;i++){
    int u,k; cin >> u >> k;
    for(int i=0;i<k;i++){
      int v; cin >> v;
      G[u].push_back(v);
    }
  }
  //bread first search
  for(int i=1;i<=n;i++){
    visited[i]=0;
    d[i]=-1;
  }
  visited[1]=1;
  d[1]=0;
  Q.push(1);
  while(!(Q.empty())){
    int u=Q.front(); Q.pop();
    for(int v=0;v<G[u].size();v++){
      if(visited[G[u][v]]==0){
        visited[G[u][v]]=1;
        d[G[u][v]]=d[u]+1;
        Q.push(G[u][v]);
      }
    }
  }
  for(int i=1;i<=n;i++){
    cout << i << " " << d[i] << endl;
  }
}

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

using namespace std;

int n;
int g[101][101] = {};
int sp[101] = {};


void bfs(){

  queue<int> q;
  q.push(1);

  while( !q.empty() ){
    int v = q.front(); q.pop();
    for(int i=1; i<=n; i++){
      if(g[v][i] == 1 && sp[i] == -1){
	q.push(i);
	sp[i] = sp[v]+1;
      }
    }
  }
}


int main(){

  memset(g,0,sizeof(g));
  memset(sp,-1,sizeof(sp));
  cin >> n;
  sp[1] = 0;

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

  bfs();

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

}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<array>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int INF = (1<<29);
int ms[100][100],n;
int d[100];


void dfs(int a){
    rep(i,n){
        d[i]=INF;
    }
    queue<int> q;
    q.push(a);
    d[a]=0;
    while(!q.empty()){
        int b=q.front();q.pop();
        for(int i=0;i<n;i++){
            if(ms[b][i]!=0&&d[i]==INF){
                d[i]=d[b]+1;
                q.push(i);
                
            }
        }
    }
    
    
    
    rep(i,n){
        cout<<i+1<<" "<<(d[i]==INF?-1:d[i])<<endl;
    }
}


int main(){
    cin>>n;
    rep(i,n){
        int a,b,c;
        cin>>a>>b;
        a--;
        rep(j,b){
            cin>>c;
            c--;
            ms[a][c]=1;
        }
    }
    
    dfs(0);
    return 0;

    
}