Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

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

#define MAX 100
int n;
int M[MAX][MAX];
int dist[MAX];

void bfs(int);

int main(){
  int u,k,v;
  int 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;
    cin >> k;
    u--;
    for( i = 0; i < n; i++){
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  return 0;
}

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

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n,v[105],d[105];
vector<int>m[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(auto i:m[u])
        {
            int vv=i;
             if(!v[vv])
            {
                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=1;i<=n;i++)
    {
        int u,v,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            cin>>v;
            m[u-1].push_back(v-1);
        }
    }
    bfs(0);
    for(int i=0;i<n;i++)cout<<i+1<<' '<<d[i]<<endl;
    return 0;
}

C++: #include <iostream>
using namespace std;

#define MAX 100
#define Q_MAX 50
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY -1

// d[i]は始点sから各頂点iまでの最短距離
int M[MAX][MAX], color[MAX], d[MAX], Q[Q_MAX], head, tail;

void enqueue(int x){
  //if(x == 18) cout << "head: " << head << ", tail: " << tail << endl;
  Q[tail] = x;
  if(tail + 1 == Q_MAX) tail = 0;
  else tail++;
}

int dequeue(){
  int x = Q[head];
  if(head + 1 == Q_MAX) head = 0;
  else head++;
  return x;
}

// 頂点番号は0-オリジン
void bfs(int n, int s){
  // 初期化
  head = 0;
  tail = 0;
  for(int i=0; i<n; i++){
    color[i] = WHITE;
    d[i] = INFTY;
  }

  color[s] = GRAY;
  d[s] = 0;
  enqueue(s);

  int u;
  while(head != tail){
    u = dequeue();
    //cout << "u: " << u+1 << endl;
    for(int v=0; v<n; v++){
      if(M[u][v] && color[v] == WHITE){
        color[v] = GRAY;
        d[v] = d[u] + 1;
        //cout << "v: " << v+1 << endl;
        enqueue(v);
      } 
    }
    color[u] = BLACK;
  }
}

int main(void){
  int n, u, k, v;
  cin >> n;

  // M[][]の初期化
  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--; // 頂点番号を0-オリジンにする。
    for(int j =0; j<k; j++){
      cin >> v;
      M[u][--v] = 1; // 頂点番号を0-オリジンにする。
    } 
  }

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

C++: #include<iostream>
#include<algorithm>
using namespace std;

struct VRTX {
	int v[100] = {}, dist = -1;
};

int n, curdist = 0;
VRTX u[100];

void breadthFirstSearch(int x, int dist);

int main() {
	int i, j, id, dim;
	
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> id >> dim;
		for (j = 0; j < dim; j++) {
			cin >> u[id].v[j];
		}
	}

	breadthFirstSearch(1, 0);

	for (i = 1; i <= n; i++) {
		cout << i << " " << u[i].dist << endl;
	}

	return 0;
}

void breadthFirstSearch(int x, int dist) {
	int i, nextdist;

	if (u[x].dist == -1)u[x].dist = dist;
	else if (u[x].dist == dist)return;
	else u[x].dist = min(u[x].dist, dist);
	
	i = 0;
	nextdist = u[x].dist + 1;
	while (u[x].v[i] != 0) {
		breadthFirstSearch(u[x].v[i], nextdist);
		i++;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <sstream>
#include <cmath>
#include <functional>
#include <fstream>
#include <cstdlib>
typedef long long ll;
const int MOD = 1e9+7;


#define REP(i, N) for (int i = 0; i < (N); i++)
#define REPP(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define VSORT(c) sort((c).begin(),(c).end())
#define SZ(x) ((int)(x).size())
//vvintを作る　マクロで　 #define vvint(N,M) vector<vector<int>>

using namespace std;


int main(){
    
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    int d[n+1];
    REPP(i,1,n+1) d[i] = -1;
    int hoge,num;
    vector<vector<int> > bfs(n+1);
    for(int i = 1; i < n+1;i++){
        cin >> hoge >> num;
        for(int l = 0; l < num;l++){
            int tmp;
            cin >> tmp;
            bfs[i].push_back(tmp);
        }
    }

    // 初期セットアップ(index=1)
    queue<pair<int, int> > queue;
    for(int i = 0; i < bfs[1].size();i++){
        queue.push(make_pair(1,bfs[1][i]));
    }
    d[1] = 0;

    while(!queue.empty()){
        if(d[queue.front().second] == -1){
            d[queue.front().second] = d[queue.front().first]+1;
            for(int i = 0; i < bfs[queue.front().second].size();i++){
                queue.push(make_pair(queue.front().second,bfs[queue.front().second][i]));
            }
        }
        queue.pop();
    }

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

    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;
    for(int j=0;j<k;j++){
      cin>>v;
      v--;
      m[u][v]=1;
    }
  }

  bfs(0);

  return 0;
}