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 <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
using namespace std;

const int N = 100;
const int INF = (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] = INF;

    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] != INF) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for(int i = 0; i < n; i++) {
        cout << i+1 << " "  << ( (d[i] == INF) ? (-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;

static const int N=100;
static const int INF=(1<<21);
int n, MA[N][N];
int d[N];

void Bfs(int s){
  queue<int> q;
  q.push(s);

  for(int i=0; i<n; i++)
  d[i]=INF;

  d[s]=0;
  int u;

  while(!q.empty())
  {
    u=q.front();
    q.pop();

    for(int v=0; v<n; v++)
    {
      if(MA[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 << " " <<((d[i] == INF) ? (-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++)
      MA[i][j]=0;
  }

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

  Bfs(0);

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   for(int i=0;i<(n);++i)
#define SORT(c)    sort((c).begin(),(c).end())
#define CLR(a)     memset((&a), 0 ,sizeof(a))//clear memory
#define dump(x)    cerr << #x << " = " << (x) << endl;//debug
#define debug(x)   cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define EPS        1e-10//sample:if((double)>=(double)+EPS)
//cin,cout release
//std::cin.tie(0);
//ios::sync_with_stdio(false);

using namespace std;

int n,v[100+1][100+1]={},d[1000]={},color[1000]={};
int q[1000]={},head=0,tail=0;
void bfs(int s){
  rep(i,n){
    if(v[s][i]==0&&s!=i){
      color[s]=0;
      d[i]=INT_MAX;
    }
  }
  color[s]=1;
  d[s]=0;
  head=tail=0;
  q[tail++]=s;
  while(head!=tail){
    int u=q[head++];
    rep(i,n){
      if(v[u][i]==0)continue;
      if(color[i]==0){
	color[i]=1;
	d[i]=d[u]+1;
	q[tail++]=i;
      }
    }
    color[u]=2;
  }
}
int main(){
  cin>>n;
  int u,k,in;
  rep(i,n){
    cin>>u>>k;
    rep(j,k){
      cin>>in;
      v[u-1][in-1]=1;
    }
  }
  //rep(i,n)
  bfs(0);
  rep(i,n){
    cout<<i+1<<" ";
    if(d[i]==INT_MAX)cout<<-1;
    else cout<<d[i];
    cout<<endl;
  }
  return 0;
}

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

using namespace std;

static const int N = 100;
static const int WHITE = 0;
static const int GREY = 1;
static const int BLACK = 2;

int n, m[N][N];
bool visited[N];
int d[N], dist;


void bfs(int i){
	queue<int> q; q.push(i);
	visited[i] = true;

	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (int v = 0; v < n; ++v) {
			if (!visited[v] && m[u][v]){
				d[v] = d[u] + 1;
				visited[v] = true;
				q.push(v);
			}
		}
	}
}


void search(){
	for (int i = 0; i < n; ++i) {
		d[i] = -1;
		visited[i] = false;
	}
	d[0] = 0;
	for (int i = 0; i < n; ++i) {
		if (!visited[i]) bfs(i);
	}
	for (int i = 0; i < n; ++i)	cout << i + 1 << " " << d[i] << endl;
}


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

	search();

	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>
#include <vector>
using namespace std;

int main () {
    int n ;
    cin >> n;

    vector <int> v[n + 1];

    for (int i = 1; i <= n; i++) {
        int x, no;
        cin >> x >> no;
        for(int j = 0; j < no; j++) {
            int temp;
            cin >> temp;
            v[x].push_back(temp);
        }
    }

	queue <int> q;
	q.push(1);
	
	int dis[n + 1];
	for (int i = 0; i <= n; i++)
		dis[i] = -1;
	dis[1] = 0;
		
	bool vis[n + 1] = {0};
    vis[1] = 1;
	while (!q.empty()) {
		for (int i = 0; i < v[q.front()].size(); i++) {
			int temp = v[q.front()][i];
			if (!vis[temp]) {
				q.push(temp);
				vis[temp] = 1;
				dis[temp] = dis[q.front()] + 1;
			}	
		}
		q.pop();
	}
	
	for (int i = 1; i <= n; i++)
		cout << i << ' ' << dis[i] << '\n';
}