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<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = 10000;
int n;
int vs[MAXN];
int d[MAXN];
void bfs(int s,vector<vector<int> > &g){
  queue<int> q;
  q.push(s);
  fill(d,d+MAXN,-1);
  d[s] = 0;
  while(!q.empty()){
    int current = q.front();q.pop();
    for(int next:g[current]){
      if(d[next] < 0){
        q.push(next);
        d[next] = d[current] + 1;
      }
    }
  }
}
int main(){
  cin >> n;
  vector<vector<int> > 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);
    }
  }
  bfs(1,g);
  for(int i = 1;i <= n;i++){
    cout << i << ' ' << d[i] << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<pair<int, int>, int> PP;
// const ll INF = 1LL << 60;
const ll MOD = 1000000007;
const int INF = 1000000000;
const int MAX = 300000;

int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
//------------------------------------------------------------------------------------------------------------------------------

vector<vector<int>> G;
vector<int> d;
bool visit[1000];
void bfs(int s) {
    queue<int> que;
    visit[s] = 1;
    d[s] = 0;
    que.push(s);
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (auto nv : G[v]) {
            if (visit[nv]) {
                continue;
            }
            visit[nv] = 1;
            d[nv] = d[v] + 1;
            que.push(nv);
        }
    }
}
int main(void) { 
    int N;
    cin >> N;
    G.resize(N);
    d.assign(N, -1);
    for (int i = 0; i < N; i++) {
        int u, k;
        cin >> u >> k;
        u--;
        for(int i = 0; i < k; i++) {
            int v;
            cin >> v;
            v--;
            G[u].push_back(v);
        }
    }
    bfs(0);
    for(int i = 0; i < N; i++) {
        cout << i + 1 << ' ' << d[i] << endl;
    }
}

C++: #include<iostream>
#include<queue>
using namespace std;
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 999999

int n,M[N][N];
int d[N];
queue<int> q;

void bfs(int s)
{
   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,node;
    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;//row
        if(i==u-1)
        {
            cin >> node;//number of node
            for(int j=0;j<node;j++)
            {
                cin >> v;
                M[i][v-1]=1;
            }
        }
    }

    /*
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cout << M[i][j] << " " ;
        }
        cout << endl;
    }*/

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

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

const int MAX = 100;
int n;
int G[MAX + 1][MAX + 1];
int states[MAX + 1];
int d[MAX + 1];
queue<int> Q;

void bfs(int id) {
  d[id] = 0;
  Q.push(id);

  while (!Q.empty()) {
    id = Q.front(); Q.pop();
    for (int i = 0; i < n; i++) {
      if (G[id][i] == 1 && states[i] == 0) {
        Q.push(i);
        states[i] = 1;
        d[i] = d[id] + 1;
      }
    }
  }
}

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

  cin >> n;

  int u, v, k;

  for (int i = 0; i < n; i++) {
    d[i] = -1;
    states[i] = 0;
    for (int j = 0; j < n; j++) {
      G[i][j] = 0;
    }
  }

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

  bfs(0);

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

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
#include <queue>

using namespace std;

const int WHITE = 0, GRAY = 1, BLACK = 2;

struct BFS {
    int n;
    vector<vector<int>> g;
    vector<int> depth;
    vector<int> state;
    int time = 0;

    BFS(int n) : n(n) {
        g.assign(n, vector<int>());
        depth.assign(n, -1);
        state.assign(n, WHITE);
    }

    void addEdge(int u, int v) {
        g[u].push_back(v);
    }

    void bfs(int s) {
        queue<int> q;
        state[s] = GRAY;
        depth[s] = 0;
        q.push(s);

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            if (state[u] == BLACK) {
                return;
            }
            for (int v : g[u]) {
                if (state[v] == WHITE) {
                    state[v] = GRAY;
                    depth[v] = depth[u] + 1;
                    q.push(v);
                }
            }
            state[u] = BLACK;
        }
    }

    void print() {
        for (int i = 0; i < n; i++) {
            cout << i << ":";
            for (int v : g[i]) {
                cout << v << " ";
            }
            cout << endl;
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    BFS dfs(n);
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            v--;
            dfs.addEdge(u, v);
        }
    }

    //dfs.print();

    dfs.bfs(0);

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

    return 0;
}