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++: //ALDS1_11_B:Depth First Search
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <tuple>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <random>
#include <time.h>
#include <cassert>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> v[101];
    for(int i=0; i<n; i++){
        int u,k;
        cin >> u >> k;
        for(int j=0; j<k; j++){
            int tmp;
            cin >> tmp;
            v[u].push_back(tmp);
        }
    }

    int d[101];
    for(int i=1; i<=n; i++) d[i] = -1;

    queue<int> que;
    que.push(1);
    int now = 0;
    while(!que.empty()){
        int tmp = que.size();
        for(int i=0; i<tmp; i++){
            if(d[que.front()] != -1){
                que.pop();
                continue;
            }

            d[que.front()] = now;
            for(int j=0; j<v[que.front()].size(); j++){
                if(d[v[que.front()][j]] == -1) que.push(v[que.front()][j]);
            }
            que.pop();
        }
        now++;
    }

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

C++: #include <iostream>
#include <queue>
#define INFTY -1
using namespace std;



int a[124][124] = {{}};
int d[124] = {};
int n;
void bfs(int k){
  queue <int> q;
  q.push(k);
  int u;


  while(!q.empty()){
    u = q.front(); q.pop();
    for(int v = 0;v < n;v++){
      if(a[u][v] == 0) continue;
      if(d[v] == INFTY) {
        d[v] = d[u] + 1;
        //cout << v << " " << d[v] << endl;
      }
      q.push(v);
    }

  }
}

int main(){

  int u,k,x;
  cin >> n;

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

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

  bfs(0);

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

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

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

int g[101][101],n,d[101],f[101];

int main() {
    cin >> n;
    for(int i = 0;i < n;i++) {
        int u,k,v;
        cin >> u >> k;
        for(int j = 0;j < k;j++) {
            cin >> v;
            g[u][v]++;
        }
    }
    queue<int> q;
    q.push(1);
    d[1] = 0;
    while(!q.empty()) {
        int u = q.front();q.pop();
        for(int i = 1;i <= n;i++) {
            if(g[u][i] && d[i] == 0)
            {
                d[i] = d[u]+1;
                q.push(i);
            }
        }
    }
    for(int i = 1;i <=n;i++) {
        cout << i << " " << d[i] <<endl;
    }
}

C++: #include<iostream>
#include<queue>
using namespace std;
int N;
int D[100];
void bfs(int src){
    int u[100],k[100];
    int d[100][100],a[100];
    bool visit[100];
    for(int i=1;i<=N;++i){
        cin>>u[i]>>k[i];
        for(int j=0;j<k[i];++j){
            cin>>a[j];
            d[u[i]][a[j]]=1;
        }
        visit[i]=true;
    }
    queue<int>Q;
    Q.push(src);
    D[src]=0;
    visit[src]=false;
    while(!Q.empty()){
        int cur=Q.front();
        Q.pop();
        for(int i=1;i<=N;++i){
            if(d[cur][i]==1&&visit[i]){
                visit[cur]=false;
                visit[i]=false;
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
    for(int i=1;i<=N;++i){
        cout<<i<<" "<<D[i]<<endl;
    }
}
int main(){
    cin>>N;
    int start=1;
    bfs(start);
}

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

#define PI 3.141592653
#define rep(i,a,n) for(int i=a;i<(int)n;++i)
#define SZ(x) ((int)(x).size())	//size() unsigned -> int
#define descSort(a) sort(a.begin(),a.end(),std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;
ll gcd(ll x,ll y){
    if(x%y==0)return y;
    return gcd(y,x%y);
}

int main(void){
    int n;
    cin>>n;
    vector<vector<bool>> graph(n,vector<bool>(n));
    rep(i,0,n){
        int u,k;
        cin>>u>>k;
        u--;
        rep(j,0,k){
            int v;
            cin>>v;
            v--;
            graph[u][v]=true;
        }
    }
    vector<int> d(n,-1);
    queue<int> q;
    q.push(0);
    d[0]=0;
    while(!q.empty()){
        int now=q.front();q.pop();
        for(int i=0;i<n;i++){
            if(graph[now][i] && d[i]==-1){
                q.push(i);
                d[i]=d[now]+1;
            }
        }
    }
    rep(i,0,n){
        cout<<i+1<< " "<< d[i]<<endl;
    }
    return 0;
}