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 <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <set>
using namespace std;
typedef unsigned long long ull;
typedef  long long ll;

void bfs(int start, int n, vector<vector<int>> &a) {
	int INF = 10000000;
	vector<int> d(n, INF);
	queue<int> q;
	d[start] = 0;
	q.push(start);
	while (q.size() != 0) {
		int u = q.front();
		q.pop();
		for (int i = 0; i < n; i++) {
			if (a[u][i] == 0)
				continue;
			if (d[i] != INF)
				continue;
			d[i] = d[u] + 1;
			q.push(i);
		}
	}
	for (int i = 0; i < n; i++) {
		if (d[i] == INF) {
			cout << i + 1 << " -1" << endl;
		}
		else {
			cout << i + 1 << " " << d[i] << endl;
		}
	}
}
int main()
{
	int n;
	vector<vector<int>> a;
	cin >> n;
	a.resize(n);
	for (int i = 0; i < n; i++) {
		a[i].resize(n);
	}
	for (int i = 0; i < n; i++) {
		int temp, k;
		cin >> temp >> k;
		for (int j = 0; j < k; j++) {
			int temp2;
			cin >> temp2;
			a[temp - 1][temp2 - 1] = 1;
		}
	}

	bfs(0, n, a);

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <cstring>

#define LP(i,n) for(int i=0;i<n;i++)
#define Mset(x,v) memset(x,v,sizeof(x))

using namespace std;
using LL=long long;
const int INF=100000000;

int A[110][110],d[100];

int main(){
    int n,u,k,v;
    cin>>n;
    LP(i,n){
        cin>>u>>k;
        LP(j,k){
            cin>>v;
            A[u-1][v-1]=1;
        }
    }
    queue<int> q;
    q.push(0);
    LP(i,n) d[i]=INF;
    d[0]=0;
    int x=0;
    while(!q.empty()){
        x=q.front();
        q.pop();
        LP(i,n){
            if(A[x][i]==0) continue;
            if(d[i]!=INF) continue;
            d[i]=d[x]+1;
            q.push(i);
        }
    }
    LP(i,n){
        cout<<i+1<<" "<<((d[i]==INF)?(-1):d[i])<<endl;
    }
    return 0;
}

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

int res[101];
int G[101][101] = {0};

int main() {
    int n, u, v, k;
    std::queue<int> Q;
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> u >> k;
        for (int j = 0; j < k; j++) {
            std::cin >> v;
            G[u - 1][v - 1] = 1;
        }
    }

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

    Q.push(0);
    res[0] = 0;

    while (! Q.empty()) {
        u = Q.front();
        for (int i = 0; i < n; i++) {
            if (G[u][i]) {
                Q.push(i);
                res[i] = std::min(res[i], res[u] + 1);
            }
        }
        Q.pop();
    }

    for (int i = 0; i < n; i++) {
        if (res[i] != INF) std::cout << i + 1 << " " << res[i] << "\n";
        else std::cout << i + 1 << " -1\n";
    }

    return 0;
}

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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
#define INF 1000000

int main()
{
  int n;cin>>n;
  int i,j;
  vector<vector<int>> g(n);
  rep(i,n)
  {
    int u,k;cin>>u>>k;
    u--;
    rep(j,k)
    {
      int vj;cin>>vj;
      vj--;
      g[u].emplace_back(vj);
    }
  }
  queue<int> q;
  queue<int> q2;
  q.push(0);
  vector<int> ans(n,INF);
  ans[0]=0;
  while(!q.empty())
  {
    while(!q.empty())
    {
      int now=q.front();
      q.pop();
      rep(i,g[now].size())
      {
        if(ans[g[now][i]]==INF)
        {
          q2.push(g[now][i]);
          ans[g[now][i]]=ans[now]+1;
        }
      }
    }
    while(!q2.empty())
    {
      q.push(q2.front());
      q2.pop();
    }
  }
  rep(i,n)
  {
    cout<<i+1<<' ';
    if(ans[i]==INF)cout<<-1<<endl;
    else cout<<ans[i]<<endl;
  }
}

C++: //単一始点最短経路
#include<iostream>
using namespace std;
static const int MAX=100;
static const int INFTY=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n,M[MAX][MAX];

void dijkstra(){
    int minv;
    int d[MAX],color[MAX];

    for(int i=0; i<n; i++){
        d[i]=INFTY;
        color[i]=WHITE;
    }

    d[0]=0;
    color[0]=GRAY;

    while(1){
        minv=INFTY;
        int u=-1;
        for(int i=0; i<n; i++){
            if(minv>d[i] && color[i]!=BLACK){
                u=i;
                minv=d[i];
            }
        }
        if(u==-1){ break; }
        color[u]=BLACK;
        for(int v=0; v<n; v++){
            if(color[v]!=BLACK &&  M[u][v]!=INFTY){
                if(d[v]>d[u]+M[u][v]){
                    d[v]=d[u]+M[u][v];
                    color[v]=GRAY;
                }
            }
        }
    }
    for(int i=0; i<n; i++){
        cout<<i<<" "<<(d[i]==INFTY ? -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]=INFTY;
        }         
    }

    int k,c,u,v;
    for(int i=0; i<n; i++){
        cin>>u>>k;
        for(int j=0; j<k; j++){
            cin>>v>>c;
            M[u][v]=c;
        }
    }

    dijkstra();
    return 0;
}