Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define INF 1000000000

class Node{//普通の木用のデータ
//ノードはポインタでもっておこう
public:
  ///ノードの番号
  int num;
  //そのノードの親
  //Node* p;(親の情報が必要ない)
  //そのノードの子(ここ、vectorで管理してる場合はintの方が扱いやすい)
  vector<int> v;
  //探索済みかどうか
  bool visit=false;
};

void breadth_search(queue<int> vs,vector<Node>& vn,vector<int>& vd){
  int d=0;//vsにすでに格納しているものは距離が決定している前提
  int l=vs.size();
  while(l!=0){
    d++;
    l=vs.size();
    for(int i=0;i<l;i++){
      int x=vs.front();
      int l_sub=vn[x].v.size();
      for(int j=0;j<l_sub;j++){
        if(vn[vn[x].v[j]].visit==false){
          vs.push(vn[x].v[j]);
          vn[vn[x].v[j]].visit=true;
          vd[vn[x].v[j]]=d;
        }
      }
      vs.pop();
    }
  }
}


int main(){
  int n;cin >> n;
  vector<Node> vn(n);for(int i=0;i<n;i++){vn[i].num=i;}
  vector<int> vd(n,-1);//深さを保存(辿れないと-1)
  queue<int> vs;//今見ているNodeのnumを入れておく

  for(int i=0;i<n;i++){
    int k;cin >> k >> k;
    for(int j=0;j<k;j++){
      int l;cin >>l;l-=1;
      //vn[l].p=&vn[i];
      vn[i].v.push_back(l);
    }
  }

  vn[0].visit=true;
  vd[0]=0;
  vs.push(0);
  breadth_search(vs,vn,vd);
  for(int i=0;i<n;i++){
    cout << i+1 << " " << vd[i] << endl;
  }



}

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

struct Node{
	int s;
	queue<int> Q;
};

int main(){
	int n;
	cin >> n;
	Node node[n + 1];
	for(int i = 1; i <= n; i++){
		int u, k, v;
		cin >> u >> k;
		node[u].s = -1;
		while(k--){
			cin >> v;
			node[u].Q.push(v);
		}
	}

	int p = 1, next, m;
	queue<int> move;
	node[p].s = 0;
	move.push(p);

	while(!move.empty()){
		p = move.front();
		//cout << "Pivot is " << p << endl;
		if(node[p].Q.empty()){
			//cout << "Nowhere to go from " << p << endl;
			move.pop();
		} else {
			m = node[p].Q.size();
			while(m--){
				next = node[p].Q.front();
				node[p].Q.pop();
				//cout << "Checking " << next << endl;
				if(node[next].s < 0){
					//cout << "Can go to " << next << endl;
					node[next].s = node[p].s + 1;
					move.push(next);
				}
			}
			move.pop();
		}
	}

	for(int i = 1; i <= n; i++)
		cout << i << " " << node[i].s << endl;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)

int M[100][100];
int d[100];
int n;
static const int INFTY = (1 << 21);

void bfs(int x) {
    queue<int> q;
    q.push(x);
    REP (i, n) d[i] = INFTY;

    d[x] = 0;
    while (!q.empty()) {
        int u;
        u = q.front();
        q.pop();
        REP(i, n) {
            if (M[u][i] == 0) continue;
            if (d[i] != INFTY) continue;
            d[i] = d[u] + 1;
            q.push(i);
        }
    }

    REP(i, n) {
        cout << i + 1 << " ";
        if (d[i] == INFTY) {
            cout << -1 << endl;
        } else {
            cout << d[i] << endl;
        }
    }
}

int main() {
    cin >> n;
    int v, m, k;
    REP(i, n) {
        cin >> v >> m;
        v--;
        REP(j, m) {
            cin >> k;
            k--;
            M[v][k] = 1;
        }
    }

    bfs(0);
    return 0;
}

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

using namespeace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];

coid dfs(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 =.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 <vector>
#include <queue>

int main()
{
	int n;
	std::cin >> n;
	std::vector<std::vector<int> > E(n); 
	for (int i = 0; i < n; i++)
	{
		int u, k;
		std::vector<int> G;
		std::cin >> u >> k;
		u--;
		for (int j = 0; j < k; j++)
		{
			int g;
			std::cin >> g;
			g--;
			G.push_back(g);
		}
		E.at(u) = G;
	}
	std::vector<int> Dist(n, -1);
	Dist.at(0) = 0;
	std::queue<int> Que;
	Que.push(0);
	while (!(Que.empty()))
	{
		int s = Que.front();
		Que.pop();
		for (int i = 0; i < E.at(s).size(); i++)
		{
			int next = E.at(s).at(i);
			if (Dist.at(next) == -1)
			{
				Dist.at(next) = Dist.at(s) + 1;
				Que.push(next);
			}
		}
	}
	for (int i = 0; i < n; i++)
		std::cout << i + 1 << ' ' << Dist.at(i) << '\n';
	return 0;
}