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

    
}