Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <utility>
#include <ctime>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define vector_rsort(x) sort(all(x));reverse(all(x))
#define array_rsort(x,n) sort(x,x+n);reverse(x,x+n);
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))

#define Yes cout<<"Yes"<<endl
#define YES cout<<"YES"<<endl
#define No cout<<"No"<<endl
#define NO cout<<"NO"<<endl
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

int n,u,k,v;
int g[105][105];

void init(){
  rep(i,105){
    rep(j,105){
      g[i][j]=INF;
    }
  }
}

void wf(){
  rep(k,105){
    rep(i,105){
      rep(j,105){
	g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
      }
    }
  }
}

int main(){
  init();
  cin>>n;
  rep(i,n){
    cin>>u>>k;
    rep(i,k){
      cin>>v;
      g[u][v]=1;
    }
  }
  wf();
  for(int i=1;i<=n;i++){
    if(g[1][i]==INF)cout<<i<<" "<<0<<endl;
    else cout<<i<<" "<<g[1][i]<<endl;
  }
  return 0;
}

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

int main(void){
  int N;
  cin >> N;
  
  vector<vector<int>> G(N);
  for(int i=0;i<N;i++){
    int u,k;
    cin >> u >> k;
    for(int j=0;j<k;j++){
      int tmp;
      cin >> tmp;
      G[i].push_back(tmp-1);
    }
  }
  
  vector<int> S(N);
  for(int i=0;i<N;i++){
    S[i] = -1;
  }
  queue<pair<int,int>> Q;
  for(auto it:G[0]){
    Q.push(make_pair(it,1));
  }
  S[0] = 0;
  while(!Q.empty()) {
    auto i = Q.front();
    Q.pop();
    if(S[i.first]>-1){
      continue;
    } else {
      S[i.first] = i.second;
    }
      
    for(auto it: G[i.first]){
      Q.push(make_pair(it,i.second+1));
    }
  }
  
  for(int i=0;i<N;i++){
    cout << i+1 << " " << S[i] << endl;
  }
}

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

int n;
bool A[105][105];
int D[105];

void bfs(int src){
    queue<int> Q;
    Q.push(src);
    D[src] = 0;
    while(!Q.empty()){
        int cur = Q.front();
        Q.pop();
        for(int dst=0; dst<105; dst++){
            if(A[cur][dst]==1 && D[dst]==-1){
                D[dst]=D[cur]+1;
                Q.push(dst);
            }
        }
        
    }
}

int main(int argc, const char * argv[]) {
    for(int i=0; i<105; i++){
        for(int j=0; j<105; j++){
            A[i][j] = 0;
        }
    }
    cin >> n;
    for(int i=0; i<n; i++){
        int u;
        int k;
        cin >> u >> k;
        u--;
        for(int j=0; j<k; j++){
            int v;
            cin >> v;
            v--;
            A[u][v] = 1;
        }
    }
    for(int i=0; i<105; i++){
        D[i] = -1;
    }
    bfs(0);
    for(int i=0; i<n; i++){
        cout << i+1 << " " << D[i] << endl;
    }
    return 0;
}

C++: #if 0
IDが1から始まることをどのように扱うかを最初にしっかり定義していなかったせいで時間がかかってしまった。
#endif

#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;

map<int, vector<int>> init(int N)
{
    map<int, vector<int>> P;
    for (int i = 0; i < N; i++)
    {
        int id, dataNum;
        cin >> id >> dataNum;
        vector<int> temp;
        for (int j = 0; j < dataNum; j++)
        {
            int tempNode;
            cin >> tempNode;
            temp.push_back(tempNode);
        }
        P[id - 1] = temp; // P[i] = 隣接ノード
    }
    return P;
}

int main()
{
    int N;
    cin >> N;
    map<int, vector<int>> P = init(N);
    int distance[N];
    for (int i = 0; i < N; i++)
    {
        distance[i] = -1;
    }
    distance[0] = 0;
    queue<int> Q = queue<int>({0});
    while (!Q.empty())
    {
        int cur = Q.front(); // 現在地
        Q.pop();
        for (int itr = 0; itr < P[cur].size(); itr++)
        {
            int dst = P[cur][itr] - 1;
            if (distance[dst] == -1)
            {
                distance[dst] = distance[cur] + 1;
                Q.push(dst);
            }
        }
    }
    for (int i = 0; i < N; i++)
    {
        cout << i + 1 << " " << distance[i] << endl;
    }
}