Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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

using namespace std;

int main(){
    int n;
    cin >> n;
    
    int g[102][102];
    int d[102];
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            g[i][j] = 0;
        }
        d[i] = -1;
    }
    
    int u,k;
    for(int i=0; i<n; i++){
        cin>>u>>k;
        for(int j=0; j<k; j++){
            int v;
            cin>>v;
            g[u][v] = 1;
        }
    }

    int q[102];
    int h = 0, t = h + 1;
    q[h] = 1;
    d[ q[h] ] = 0;
    while(h != t){
        u = q[h++];
        for(int i=1; i<=n; i++){
            if(g[u][i] == 1 && d[i] == -1){
                g[u][i] = 0;
                d[i] = d[u]+1; 
                q[t++] = i;
              
            }
        }
    }
    
    for(int i=1; i<=n; i++){
        cout<< i << " " << d[i] << endl;
    }
    
    return 0;
}

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

int main() {
  int n;
  cin >> n;

  vector<vector<int>> M(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    int node;
    int d;
    cin >> node >> d;
    node--;

    for (int j = 0; j < d; j++) {
      int tmp;
      cin >> tmp;
      tmp--;
      M[i][tmp] = 1;
    }
  }

  vector<int> res(n);
  vector<int> e(n);

  queue<int> q;
  q.push(0);

  while (!q.empty()) {
    int num = q.front();
    q.pop();

    int cnt = res[num] + 1;

    for (int i = 0; i < M[num].size(); i++) {
      if (M[num][i] == 0) { continue; }
      if (e[i] == 1) { continue; }
      q.push(i);
      e[i]   = 1;
      res[i] = cnt;
    }
  }

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

C++: #include<bits/stdc++.h>
using namespace std;
const int maxn=100+10;
int n,G[maxn][maxn],dis[maxn],vis[maxn];
void bfs(){
    memset(dis,0,sizeof dis);
    memset(vis,0,sizeof vis);
    queue<int>que;
    que.push(1);vis[1]=1;dis[1]=0;
    while(!que.empty()){
        int cur=que.front();que.pop();
        for(int i=1;i<=n;i++){
            if(G[cur][i]&&!vis[i]){
                que.push(i);
                dis[i]=dis[cur]+1;
                vis[i]=1;
            }
        }
    }
}
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 v;cin>>v;
            G[u][v]=1;
        }
    }
    bfs();
    for(int i=1;i<=n;i++) if(!vis[i]) dis[i]=-1;
    for(int i=1;i<=n;i++) cout<<i<<" "<<dis[i]<<"\n";
    /*
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<G[i][j]<<" ";
        }cout<<endl;
    }*/
}
/*
4
1 2 2 4
2 1 4
3 0
4 1 3
*/

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'

signed main() {
    int n;
    cin >> n;

    vector<int> d(n);
    vector<vector<int> > G(n);

    for(int i = 0; i < n; i++) {
        int itr,m;
        cin >> itr >> m;
        for (int j = 0; j < m; j++) {
            int v;
            cin >> v;
            G[itr-1].push_back(v-1);
        }
    }
    const int N = 110;
    rep(i,n) d[i] = N;
    queue<int> Q;
    Q.push(0);
    d[0] = 0;
    while(!Q.empty()){
        int x = Q.front();
        Q.pop();
        for(int j = 0; j < G[x].size(); j++){
            int to = G[x][j];
            if(d[to] == N) {
                Q.push(to);
                d[to] = min(d[to], d[x] + 1);
            }
        }
    }

    for(int i = 0; i < n; i++){
        int v = d[i];
        if(v == N) v = -1;
        cout << i + 1 << " " << v << endl;
    }
}

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

int main(){
    int n;
    cin >> n;
    int g[110][110];
    int i, j;
    int u, k, v;
    int dist[110];
    bool visited[110];
    
    for(i = 0; i < n; i++){
        dist[i] = -1;
        for(j = 0; j < n; j++){
            g[i][j] = 0;
        }
    }
    
    for(i = 0; i < n; i++){
        cin >> u >> k;
        u--;
        for(j = 0; j < k; j++){
            cin >> v;
            v--;
            g[u][v] = 1;
        }
    }
    
    queue<int> q1, q2;
    q1.push(0);
    
    for(i = 0; q1.size(); i++){
        while(!q1.empty()){
            int p = q1.front();
            q1.pop();
            
            if(visited[p]) continue;
            visited[p] = true;
            
            dist[p] = i;
            
            for(j = 0; j < n; j++){
                if(g[p][j]){
                    q2.push(j);
                }
            }
        }
        q1 = q2;
        while(!q2.empty()){
            q2.pop();
        }
    }
    
    for(i = 0; i < n; i++){
        printf("%d %d\n", i+1, dist[i]);
    }
}

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