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

#define rep(i,n) for (int i = 0; i < int(n); i++)
#define show(x) #x " = " << x

int n;
vector<int> G[100];
int d[100];

int main() {
    cin >> n;
    rep (i,n) {
        int u, k, v;
        cin >> u >> k;
        rep (i,k) cin >> v, G[u-1].push_back(v-1);
    }
    rep (i,100) d[i] = -1;

    queue<int> q;
    q.push(0);
    d[0] = 0;
    while (!q.empty()) {
        int v = q.front(); q.pop();
        rep (i,G[v].size()) {
            int x = G[v][i];
            if (d[x] == -1) {
                d[x] = d[v] + 1;
                q.push(x);
            }
        }
    }
    rep (i,n) cout << i+1 << " " << d[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 <iostream>
using namespace std;
#define INF 9999999

int dis[101]; //距離を格納
bool check[101];//訪問済みチェック
int main(){
  int n;
  cin>>n;
  fill(dis,dis+101,INF);
  dis[1]=0;
  check[1]=true;
  for(int i=0;i<n;i++){
    int u,k,v;
    cin>>u>>k;
    for(int j=1;j<=k;j++){
      cin>>v;
      if(check[v]){//訪問済みならば
        continue;
      }else{
        check[v]=true;
      }
      dis[v]=min(dis[u]+1,dis[v]);
    }
  }

  //表示部
  for(int i=1;i<=n;i++){
    cout<<i<<" ";
    if(dis[i]==INF)cout<<-1<<"\n";
    else  cout<<dis[i]<<"\n";
  }
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
static const int MAX=10000;
static const int INFTY = (1<<20);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n;
vector<pair<int,int> > adj[MAX];
void dijkstra(){
  priority_queue<pair<int,int> > PQ;
  int color[MAX];
  int d[MAX];
  for(int i=0;i<n;i++){
    d[i]=INFTY;
    color[i]=WHITE;
  }
  d[0]=0;
  PQ.push(make_pair(0,0));
  color[0]=GRAY;

  while(!PQ.empty()){
    pair<int,int>f=PQ.top();PQ.pop();
    int u=f.second;
    color[u]=BLACK;
    if(d[u]<f.first*(-1))continue;
    for(int j=0;j<adj[u].size();j++){
      int v=adj[u][j].first;
      if(color[v]=BLACK)continue;
      if(d[v]>d[u]+adj[u][j].second){
        d[v]=d[u]+adj[u][j].second;
        PQ.push(make_pair(d[v]*(-1),v));
        color[v]=GRAY;
      }
    }
  }
  for(int i=0;i<n;i++){
    cout<<i<<" "<<(d[i]==INFTY ? -1:d[i])<<endl;
  }
}

int main(){
  int k,u,v,c;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>u>>k;
    for(int j=0;j<k;j++){
      cin>>v>>c;
      adj[u].push_back(make_pair(v,c));
    }
  }
  dijkstra();
  return 0;
}

C++: #include<iostream>
#include<queue>
#define N 101
using namespace std;
int A[N][N];
bool visited[N];
int n;
int d[N];
int distance;
queue<int> node;

void bfs(){
    while(!node.empty()){
        int temp=node.front();
        for(int i=0;i<n;i++){
            if(temp==i) continue;
            if(!visited[i] && A[temp][i]==1){
                d[i]=d[temp]+1;
                visited[i]=true;
                node.push(i);
            }
        }
        node.pop();
    }
}

int main(){
    int k,num;
    int e;
    cin>>n;
    for(int i=0;i<n;i++){
        visited[i]=false;
        d[i]=-1;
        for(int j=0;j<n;j++)
            A[i][j]=0;
    }
    for(int i=0;i<n;i++){
        cin>>k;
        k--;
        cin>>num;
        for(int j=0;j<num;j++){
            cin>>e;
            e--;
            A[k][e]=1; //A[e][k]=1;
        }
    }

    node.push(0);
    visited[0]=true;
    d[0]=0;
    bfs();

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

    return 0;

}

C++: #include<iostream>
using namespace std;
#include<queue>
queue<int>mp;
#define N 105
#define INF 1<<21
int a[N][N];
int d[N];
int n;

void bfs(int s){
	mp.push(s);
	for (int i = 1;i <= n;i++){
		d[i] = INF;
	}
	d[s] = 0;
	int u;
	while (!mp.empty()){
		u = mp.front();
		mp.pop();
		for (int v = 1;v <= n;v++){
			if (a[u][v] == 0) continue;
			if (d[v] != INF) continue;
			d[v] = d[u] + 1;
			mp.push(v);
		}
	}
	for (int i = 1 ;i <= n;i++){
		if (d[i] == INF){
			cout << i << " " << "-1" << endl; 
		}
		else{
			cout << i << " " << d[i] << endl;
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 1;i <= n;i++){
		for (int j = 1;j <= n;j++){
			a[i][j] = 0;
		}
	}
	int x,m,y;
	for (int i = 1;i <= n;i++){
		cin >> x >> m;
		while (m--){
			cin >> y;
			a[x][y] = 1;
		}
	}
	bfs(1);
	return 0;
}