Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<int, int>
#define F first
#define S second
#define prime 1000000007
#define INF 40000000000000000
int dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dx4[4] = {-1, 0, 1, 0};
int dy4[4] = {0, -1, 0, 1};
signed main(void) {
    int n;
    cin >> n;
    vector<int> Graph[n];
    rep(i, n) {
        int k;
        cin >> k >> k;
        rep(j, k) {
            int a;
            cin >> a;
            Graph[i].push_back(a - 1);
        }
    }

    vector<int> dist(n, -1);
    dist[0] = 0;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()) {
        int now = Q.front();
        Q.pop();
        for(auto x : Graph[now]) {
            if(dist[x] != -1) continue;
            dist[x] = dist[now] + 1;
            Q.push(x);
        }
    }
    rep(i, n) cout << i + 1 << ' ' << dist[i] << endl;
}

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

#define MAX 200
int n;
vector<vector<int>> adj_mat(MAX, vector<int>(MAX, 0));

queue<int> q;
vector<int> visited(MAX, -1);
vector<int> d(MAX, -1);

void add_queue(int node){
    // cout << "node " << node << " unvisited & adj";
    rep(i, n){
        if(adj_mat.at(node).at(i) == 1 && visited.at(i) == -1){
            // nodeに隣接し、まだ訪れていないノード
            q.push(i);
            d.at(i) = d.at(node) + 1;
            visited.at(i) = 1;
            // cout << " " << i;

        }
    }
    // cout << endl;
}

void bfs(int node){
    q.push(node);
    d.at(node) = 0;
    visited.at(node) = 1;
    while(q.size() != 0){
        int visited_node = q.front();
        q.pop();
        // visited.at(visited_node) = 1;
        add_queue(visited_node);

    }
}

int main(){
    cin >> n;

    // 隣接行列の作成
    rep(i, n){
        int u, k; cin >> u >> k;
        rep(j, k){
            int v; cin >> v;
            adj_mat.at(u-1).at(v-1) = 1;
        }
    }

    bfs(0);
    rep(i, n){
        cout << i+1 << " " << d.at(i) << endl;
    }

}

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 argc, char const *argv[])
{
    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++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vsi = vector<queue<int>>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

void bfs(vsi &g, vi &d, queue<int> &q, int v, int depth) {
  queue<int> nq;
  while (!q.empty()) {
    int f = q.front(); q.pop();

    while (!g[f].empty()) {
      nq.push(g[f].front()); g[f].pop();
    }

    if (d[f] == -1) d[f] = depth;
  }
  
  q = nq;
  if (q.empty()) return;
  bfs(g, d, q, q.front(), depth + 1);
}

int main() {
  int n;
  cin >> n;
  
  vsi g(n);
  rep(i, n) {
    int k, u;
    cin >> k >> u;

    rep(j, u) {
      int v;
      cin >> v;

      g[i].push(v - 1);
    }
  }

  vi d(n, -1);
  queue<int> q;
  q.push(0);
  bfs(g, d, q, 0, 0);

  rep(i, n) printf("%d %d\n", i + 1, d[i]);
}