Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

func pop(q *[]int) int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

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

queue<pair<int,int> > qu;
vector<int> v[111];
int root;
int b;
int d[111];
pair<int, int> pa;
int g;

void wide(int num){

  memset(d,-1,sizeof(d));
  d[num] = 0;
  qu.push(make_pair(num,d[1]));

  while(!qu.empty()){

    pa = qu.front();
    qu.pop();
    int id = pa.first;

    for(int i = 0; i < (int)v[id].size(); i++){
      if(d[v[id][i]] == -1){
	d[v[id][i]] = pa.second + 1;
	qu.push(make_pair(v[id][i],d[v[id][i]]));
      }
    }
  }
  for(int i = 1; i <= g; i++){
    cout << i << " " << d[i] << endl;
  }

}

int main(){

 
  cin >> g;
  int n;
  for(int i = 0; i < g; i++){
    cin >> n >> b;
    for(int j = 0; j < b; j++){
      cin >> root;
      v[n].push_back(root);
    }
  }

  wide(1);

}

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

using namespace std;

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

void bfs (int s) {

    int i, u, v;

    queue<int> Q;
    Q.push(s);
    for (i = 0; i < n; i++) {
        d[i] = (1 << 21);
    }
    d[s] = 0;
    while (!Q.empty()) {
        u = Q.front();
        Q.pop();
        for (v = 0; v < n; v++) {
            if (M[u][v] == 0) {
                continue;
            }
            if (d[v] != (1 << 21)) {
                continue;
            }
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for (i = 0; i < n; i++) {
        cout << i + 1 << " " << ((d[i] == (1 << 21)) ? (-1) : d[i]) << endl;
    }
}

int main () {

    int u, k, v, i, j;

    cin >> n;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            M[i][j] = 0;
        }
    }

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

    bfs (0);

    return 0;

}

C++: #include <stdio.h>
#include <algorithm>

using namespace std;

struct Info{
	Info(){m_node = m_distance = 0;}
	Info(int node,int distance){
		m_node = node;
		m_distance = distance;
	}
	int m_node,m_distance;
};

class InfoQueue{
public:
	InfoQueue(){
		info = new Info[10000];
		num_of_data = head = tail = 0;
	}
	bool isEmpty(){
		return num_of_data == 0;
	}
	void enqueue(Info data){
		info[tail] = data;
		tail = (tail+1)%10000;
		num_of_data++;
	}
	Info dequeue(){
		Info ret = info[head];
		head = (head+1)%10000;
		num_of_data--;
		return ret;
	}
private:
	Info* info;
	int head,tail,num_of_data;
};


int main(){

	int n,row,col,num;
	scanf("%d",&n);

	int table[n+1][n+1];
	int check_table[n+1];

	for(int i=1;i<=n;i++){
		check_table[i] = -1;
		for(int k=1;k<=n;k++)table[i][k] = 0;
	}

	for(int i=1;i<=n;i++){
		scanf("%d %d",&row,&num);
		for(int k=1;k<=num;k++){
			scanf("%d",&col);
			table[row][col]=1;
		}
	}

	check_table[1] = 0;

	InfoQueue Q;
	Q.enqueue(Info(1,0));

	Info tmp;
	while(!Q.isEmpty()){
		tmp = Q.dequeue();
		for(int i=1;i<=n;i++){
			if(table[tmp.m_node][i]==1 && check_table[i] == -1){
				check_table[i] = tmp.m_distance+1;
				Q.enqueue(Info(i,tmp.m_distance+1));
			}
		}
	}

	for(int i=1;i<=n;i++){
		printf("%d %d\n",i,check_table[i]);
	}

    return 0;
}

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

static const int N = 100;
static const int INF = (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] = INF;
	}
	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] != INF) continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	for (int i = 0; i < n; i++) {
		cout << i + 1 << " " << ((d[i] == INF) ? (-1) : d[i]) << endl;
	}
}

int main(void) {
	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;
static const int N = 100;
static const int INFTY = (1<<21);

int n, G[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(G[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++) G[i][j] = 0;
  }

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

  bfs(0);
  return 0;
}

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

using namespace std;

static const int N=100;
static const int INF=-1;
int M[N][N],n;
int d[N];

void bfs(int s){
  queue<int> q;
  q.push(s);

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

  d[s]=0;

  while(!q.empty()){
    int u=q.front();
    q.pop();
    for(int v=0;v<n;v++){
      if(M[u][v]==0) continue;
      if(d[v]!=INF) continue;
      d[v]=d[u]+1;
      q.push(v);
    }
  }
}

int main(){
  cin>>n;

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      M[i][j]=0;
    }
  }

  int u,k,v;

  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);

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

C++: #include <iostream>
using namespace	std;
int n,g[100][100],f[100],c[100];
void bfs(int now,int far){
  f[now]=far;
  c[now]=1;
  far++;
  for(int i=0;i<n;i++) if(g[now][i]&&f[i]!=1) bfs(i,far);

}
int main(){

  int a,b,tmp;
  cin>>n;
  for(int i=0;i<n;i++){
    f[i]=-1;
    cin>>a>>b;
    for(int j=0;j<b;j++){
      cin>>tmp;
      g[a-1][tmp-1]=1;
    }
  }

  bfs(0,0);

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

  return 0;

}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int a[100],d[100],f[100];
int inf=100000,t=0,n;
int v[100][100];
void bfs(int s){
    d[s]=0;
    a[s]=inf;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=0;i<n;i++){
            if(v[u][i]==1&&a[i]!=inf){
                a[i]=inf;
                q.push(i);
                d[i]=d[u]+1;
                //cout<<d[i]<<" "<<d[u]<<endl;
            }
        }
    }
}
int main(){
  cin>>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;
      v[u-1][tmp-1]=1;
    }
  }
  bfs(0);
  for(int i=0;i<n;i++)
      if(!a[i])d[i]=-1;
  for(int i=0;i<n;i++){
        cout<<i+1<<" "<<d[i]<<endl;
  }
}