Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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;

    
}