Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

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

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

    vector<int> dist(n + 1, -1);
    queue<int> que;

    dist[1] = 0;
    que.push(1);

    while (!que.empty())
    {
        int v = que.front();
        que.pop();

        for(int nv : G[v]){
            if(dist[nv] != -1) continue;

            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    
    for (int i = 1; i <= n; i++)
    {
        cout << i << " " << dist[i] << endl;
    }
    
}

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

  int a,b,tmp;
  cin>>n;
  for(int i=0;i<n;i++){
    if(i) f[i]=1000;
    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++) if(f[i]==1000) f[i]=-1;
  for(int i=0;i<n;i++) cout<<i+1<<" "<<f[i]<<endl;

  return 0;

}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
// using ll = long long;
// using P = pair<int, int>;

bool visited[105];
int ans[105];
vector<vector<int> > v(105);

int main() {
    int n;
    cin >> n;
    
    int x, y;
    rep(i, n){
        cin >> x >> y;
        rep(j, y){
            int t;
            cin >> t;
            v[x-1].push_back(t-1);
        }
    }
    
    rep(i, 105){
        visited[i] = false;
        ans[i] = -1;
    }
    
    queue<pair<int, int> > q;
    q.push(make_pair(0, 0));
    visited[0] = true;
    
    while(!q.empty()){
        int node = q.front().first;
        int t_found = q.front().second;
        q.pop();
        rep(i, (int)v[node].size()){
            if(!visited[v[node][i]]){
                q.push(make_pair(v[node][i], t_found+1));
                visited[v[node][i]] = true;
            }
        }
        ans[node] = t_found; 
    }
    
    rep(i, n){
        cout << i+1 << " " << ans[i] << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include<queue>
using namespace std;
const int MAXn=100;
const int INFTY=1<<21;

int n,M[MAXn][MAXn]={0},d[MAXn];

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

int main() {
	cin>>n;
	int id,k,v;
	for(int i=0;i<n;i++)
	{
		cin>>id>>k;
		id--;
		for(int j=0;j<k;j++)
		{
			cin>>v;
			M[id][--v]=1;
		}
	}
	bfs(0);
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <list>
#include <cstdlib>

const int MAX_N = 100;

struct Node {
  int v;
  int d;
  bool visited;
};

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);  

  int n = 0;
  std::cin >> n;

  std::vector<std::vector<int> *> *graph = new std::vector<std::vector<int> *>(n + 1);
  std::vector<struct Node *> *nodes = new std::vector<struct Node *>(n + 1);
  
  for (int i = 1; i <= n; i++) {
    int v = 0;
    std::cin >> v;

    int k = 0;
    std::cin >> k;

    std::vector<int> *neighbors = new std::vector<int>(k);

    for (int i = 0; i < k; i++) {
      int a = 0;
      std::cin >> a;

      neighbors->at(i) = a;
    }

    graph->at(i) = neighbors;

    struct Node *node = (struct Node *) std::malloc(sizeof(struct Node));

    node->v = i;
    node->d = -1;
    node->visited = false;
    
    nodes->at(i) = node;
  }

  int count = 0;
  int start = 1;

  //  nodes->at(start)->d = 1;

  std::list<int> *stack = new std::list<int>();

  stack->push_back(nodes->at(start)->v);

  while (!stack->empty()) {
    std::vector<int> *newNodes = new std::vector<int>();

    while (!stack->empty()) {    
      int v = stack->front();
      stack->pop_front();

      nodes->at(v)->d = count;
      nodes->at(v)->visited = true;
      
      std::vector<int> *neighbors = graph->at(v);

      for (std::vector<int>::iterator iterator = neighbors->begin(); iterator != neighbors->end(); ++iterator) {
	struct Node *node = nodes->at(*iterator);

	if (!node->visited) {
	  newNodes->push_back(node->v);
	}
      }
    }

    for (std::vector<int>::iterator iterator = newNodes->begin(); iterator != newNodes->end(); ++iterator) {
      stack->push_front(*iterator);
    }

    count++;
  }

  for (std::vector<struct Node *>::iterator iterator = nodes->begin() + 1; iterator != nodes->end(); ++iterator) {
    struct Node *node = *iterator;
        
    std::cout << node->v << " " << node->d << std::endl;
  }
  
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010


int main(){
  int n;
  bool connected[200][200] = {};
  
  scanf("%d", &n);

  for(int i=0;i<n;i++){
    int u, k, v;

    scanf("%d%d", &u, &k);
    u--;
    
    for(int j=0;j<k;j++){
      scanf("%d", &v);
      v--;

      connected[u][v] = true;
    }
  }

  queue<int> q1, q2;
  bool visited[200] = {};
  int ans[200];

  for(int i=0;i<n;i++) ans[i] = -1;
  
  q1.push(0);

  for(int i=0;q1.size();i++){

    while(q1.size()){
      int p = q1.front();
      q1.pop();

      if(visited[p]) continue;
      visited[p] = true;

      ans[p] = i;
      
      for(int j=0;j<n;j++){
        if(connected[p][j]){
          q2.push(j);
        }
      }
    }

    q1 = q2;
    q2 = queue<int>();
  }

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