Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1) // 1-indexed
	d := make([]int, n+1)   // 1-indexed
	for i := 1; i <= n; i++ {
		d[i] = -1
		var u, k, v int
		fmt.Scan(&u, &k)
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			g[u] = append(g[u], v)
		}
	}

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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

int n;
int d[101];
bool a[101][101];
const int INF = (1<<21);

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())
    {
        int u = q.front();
        q.pop();
        for(int v=0; v<n; v++)
        {
            if(a[u][v] && d[v] == INF)
            {
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }
}


int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            a[i][j] = false;
        }
    }

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

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

C++: #include <cstdio>
#include <cstdlib>
#include <string>
#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;i<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 <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll

static const int N = 100;
static const int no = -1;

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll graph[N][N] = {{}}, d[N] = {}, n;

void bfs(int v) {
  queue<int> q;
  q.push(v);
  ll u;
  for(int i = 1; i <= n; i++) d[i] = no;
  d[v] = 0;
  while(!q.empty()) {
    u = q.front(); q.pop();
    for(int i = 1; i <= n; i++) {
      if(graph[u][i] == 0) continue;
      if(d[i] != no) continue;
      d[i] = d[u] + 1;
      q.push(i);
    }
  }
}

signed main() {
	ll u, v, k;
  cin >> n;
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v;
      graph[u][v] = 1;
    }
  }

  bfs(1);

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

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int N=100,INFTY=(1<<21);
int n,M[N][N],d[N];
void bfs(int s){
    std::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(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);
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
using P = pair<int, int>;
#include<queue>
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N]; // 距離で訪問状態（color）を管理する

void bfs(int s) {
    queue<int> q; // 標準ライブラリの queue を使用
    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++: //
// Created by 李青坪 on 2018/11/3.
//

#include <cstdio>
#include <algorithm>
#include <queue>

#define MAXN 101
using namespace std;
int n;
int graph[MAXN][MAXN];
int dist[MAXN];
bool visited[MAXN];
queue<int > q;

void init(){
    fill(dist, dist+MAXN, -1);
    fill(visited, visited+MAXN, false);
    dist[1] = 0;
}

void bfs(int i){
    for (int j = 1; j <= n; ++j) {
        if (graph[i][j] == 1){
            if (visited[j]) continue;
            q.push(j);
            visited[j] = true;
            dist[j] = dist[j] >= 0 ? min(dist[j], dist[i] + 1): dist[i] + 1;
        }
    }
    while (!q.empty()){
        int next = q.front();
        q.pop();
        bfs(next);
    }
}

int main(){
    scanf("%d", &n);
    int u, k;
    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &u, &k);
        int node;
        for (int j = 0; j < k; ++j) {
            scanf("%d", &node);
            graph[u][node] = 1;
        }
    }
    init();
    bfs(1);
    for (int l = 1; l <= n; ++l) {
        printf("%d %d\n", l, dist[l]);
    }
}
/*
5
1 2 2 4
2 1 4
3 0
4 1 3
5 1 4
 */