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>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<long long,int> P;



int main(){
    int n;
    vector<int> g[105];
    cin >> n;
    rep(i, n){
        int a, b;
        cin >> a >> b;
        rep(j, b){
            int c;
            cin >> c;
            g[a].push_back(c);
        }
    }
    queue<int> que;
    int dist[105];
    rep(i, n + 1) dist[i] = -1;
    dist[1] = 0;
    que.push(1);

    while(!que.empty()){
        int v = que.front();
        que.pop();
        for (auto nv : g[v]){
            if (dist[nv] != -1) continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    rep(i, n) cout << i + 1 << " " << dist[i+1] << endl;

    return 0;
}

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

const int MAX_N = 100;

int main() {
    vector<int> G[MAX_N];
    int N; cin >> N;
    for(int i = 0; i < N; i++) {
        int u, k; cin >> u >> k; u--;
        while(k--) {
            int v; cin >> v; v--;
            G[u].push_back(v);
        }
    }

    vector<int> dist(N, -1);
    queue<int> Q;
    Q.push(0);
    dist[0] = 0;

    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        for(int i = 0; i < (int)G[u].size(); i++) {
            int v = G[u][i];
            if(dist[v] != -1) continue;
            dist[v] = dist[u] + 1;
            Q.push(v);
        }
    }
    for(int i = 0; i < N; i++) cout << i + 1 << ' ' << dist[i] << endl;
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
static const int MAX=101;

vector<int> Adjacency[MAX];
int dt[MAX];
bool Visited[MAX];

void bfs(int i)
{
    queue<int> Q;
    dt[i]=0;Q.push(i);
    Visited[i]=true;
    int d=0;
    while(!Q.empty())
    {
        int p=Q.front();Q.pop();d=dt[p]+1; //important
        for(int j=0;j<Adjacency[p].size();j++)
        {
            if(Visited[Adjacency[p][j]]==true) continue;
            //else
            Q.push(Adjacency[p][j]); Visited[Adjacency[p][j]]=true;
            dt[Adjacency[p][j]]=d;
        }
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) dt[i]=-1;
    for(int i=1;i<=n;i++)
    {
        int j,num;
        cin>>j>>num;
        for(int k=0;k<num;k++)
        {
            int t;cin>>t;
            Adjacency[j].push_back(t);
        }
    }
    bfs(1);
    for(int i=1;i<=n;i++)
    {
        cout<<i<<' '<<dt[i]<<endl;
    }
}

C++: #define scanf_s scanf
#include <string>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
using namespace std;

#define MAX 100
//#define MAX_ 1000
struct Graph
{
	int id, k;
	list<int> node;
	int d = -1;
};
Graph G[MAX];
//int cou = 0;
void Breadth_cou(Graph *g,int cou)
{
	for (auto i = g->node.begin(); i != g->node.end(); ++i) {
		if (G[*i].d == -1)
			G[*i].d = cou;	
	}
	g->k = 0;
}
void Breadth(Graph *g)
{
	if (g->k > 0) {
		Breadth_cou(g, g->d + 1);
		for (auto i = g->node.begin(); i != g->node.end(); ++i) {
			Breadth(&G[*i]);
		}
	}
}

int main(void)
{
	int n, u, xk, v;
	scanf_s("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf_s("%d %d", &u, &xk);
		G[u - 1].id = u - 1;
		G[u - 1].k = xk;
		for (int j = 0; j < xk; ++j) {
			scanf_s("%d", &v);
			G[u - 1].node.push_back(v - 1);
		}
	}
	G[0].d = 0;
	Breadth(&G[0]);
	for (int i = 0; i < n; ++i) {
		printf("%d %d\n", G[i].id + 1, G[i].d);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAXN = 100+7;
int m[MAXN][MAXN];
queue<int> q;
int d[MAXN];
bool v[MAXN];
int n;
void bfs(int start) {
    d[start] = 0;
    v[start] = true;
    q.push(start);
    int u = 1;
    while(!q.empty()) {
        int x = q.front();
        int i;
        for(i = 1; i <= n; i++) {
            if(m[x][i]==1 && v[i] == false) {
                v[i] = true;
                d[i] = d[x]+1;
                q.push(i);
            }
        }
        q.pop();
    }
}
int main() {
    scanf("%d", &n);
    memset(m, 0, sizeof(m));
    memset(d, -1, sizeof(d));
    memset(v, false, sizeof(v));

    int i;
    for(i = 0; i < n; i++) {
        int num;
        int k;
        scanf("%d", &num);
        scanf("%d", &k);
        int j;
        for(j = 0; j < k; j++) {
            int id;
            scanf("%d", &id);
            m[num][id] = 1;
        }
    }
    bfs(1);
    for(i = 1; i <= n; i++){
        printf("%d %d\n", i, d[i]);
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;
class Queue{
private:
  int inf[MAX];
  int head,tail;
 
public:
 
  Queue(){
    syokika();
  }
 
  void syokika(){
    head = 0;
    tail = 0;
  }
 
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
 
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
 
  int front(){
    return inf[head];
  }
 
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
 
  int size(){
    return tail - head;
  }
 
};

int imap[200][200];
int k[200] = {0}; 
int ans[200] = {0};
bool used[200] ={false};

 



int main(){
  int n,u[1000];
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF;  
  for(int i=0;i<n;i++){
    cin >> u[i];
    cin >> k[u[i]];
    for(int j=0;j<k[u[i]];j++){
      cin >> imap[u[i]][j];
    }
  }
  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<k[i]){


    }
    cout << endl;
  }
*/
  Queue que = Queue();

  que.push(1);

  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
	que.push(imap[p][i]);
	used[imap[p][i]] = true;
	ans[imap[p][i]] = ans[p] + 1;
      }
    }


  }


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


  return 0;
}