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>

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;

vector<int> v[101];
vector<int> dist;
queue<int> q;

int bfs() {
    dist[1] = 0;
    q.push(1);

    while (!q.empty()) {
        int c = q.front();
        q.pop();
        for (int i = 0; i < v[c].size(); i++) {
            if (dist[v[c][i]] == -1) {
                dist[v[c][i]] = dist[c] + 1;
                q.push(v[c][i]);
            }
        }
    }
}

int main() {
    int n;
    cin >> n;
    dist.resize(n + 1, -1);

    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int a;
            cin >> a;
            v[u].push_back(a);
        }
    }

    bfs();

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

    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using qi = queue<int>;
using Graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)

qi que;
vi dist;

void bfs(Graph &G) {
    while (!que.empty()) {
        int v = que.front();
        que.pop();

        for(int next_v : G[v]) {
            if (dist[next_v] != -1) continue;
            que.push(next_v);
            dist[next_v] = dist[v] + 1;
        }
    }
}

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

    int n; cin >> n;
    Graph G(n);
    REP(i, n) {
        int u; cin >> u;
        int k; cin >> k;
        REP(j, k) {
            int v; cin >> v;
            G[i].push_back(v-1);
        }
    }

    que.push(0);
    dist.assign(n, -1);
    dist[0] = 0;
    bfs(G);

    REP(i, n) {
        cout << (i+1) << " " << dist[i] << "\n";
    }
}

C++: #include <stdio.h>
#include <iostream>
using namespace std;

class Adjl{
private:
	int u;
	int k;
	Adjl **next;
public:
	Adjl(int u);
	~Adjl(){ if (next != NULL) delete[] next; }
	void setV(int k, Adjl *vk);
	int breadthFirstSearch(int id);
	void print(int id);
};

Adjl::Adjl(int u)
{
	this->u = u;
	k = 0;
	next = NULL;
}

void Adjl::setV(int k, Adjl *vk)
{
	if (this->k == 0) next = new Adjl*[k];
	next[this->k] = vk;
	(this->k)++;
}

int Adjl::breadthFirstSearch(int id)
{
	if (id == u) return 0;
	int d_min = -1;
	for (int i = 0; i < k; i++) {
		int d = next[i]->breadthFirstSearch(id);
		if (d == -1) continue;
		if (d_min == -1) d_min = d + 1;
		else d_min = min(d + 1, d_min);
	}
	return d_min;
}

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(id) << endl;
}

int main(void)
{
	int n;
	cin >> n;
	
	Adjl **adjl = new Adjl*[n];
	int check[100] = {0};

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		if (check[i] == 0) {
			adjl[i] = new Adjl(u);
			check[i] = 1;
		}
		check[i] = 1;
		for (int j = 0; j < k; j++) {
			int vk;
			cin >> vk;
			if (check[vk - 1] == 0) {
				adjl[vk - 1] = new Adjl(vk);
				check[vk - 1] = 1;
			}
			adjl[i]->setV(k, adjl[vk - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		adjl[0]->print(i);
	}

	delete[] adjl;
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
vector<vector<int> >vec;
int n;
vector<int>d;
void bfs(int s) {
	queue<int>q;
	q.push(s);
	d[s] = 0;
	while (!q.empty()) {
		int u = q.front(); q.pop();
		rep(v, 0, vec[u].size()) {
			if (d[vec[u][v]] != INF)continue;
			d[vec[u][v]] = d[u] + 1;
			q.push(vec[u][v]);
		}
	}
	rep(i, 0, n) {
		cout << i + 1 << " " << d[i] << endl;
	}
}
signed main() {
	cin >> n; vec = vector<vector<int> >(n); d = vector<int>(n, INF);
	rep(i, 0, n) {
		int u, k; cin >> u >> k; u--;
		rep(j, 0, k) {
			int v; cin >> v; v--;
			vec[u].push_back(v);
		}
	}
	bfs(0);

	return 0;
}

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

int main(){
	int n;
	cin >> n;
	vector<int> G(n,-1);
	vector<vector<int>> vs(n);
	int u,k,v;
	for(int loop=0;loop<n;loop++){
		cin >> u >> k;
		for(int loop2=0;loop2<k;loop2++){
			cin >> v;
			vs[u-1].push_back(v-1);
		}
	}
	queue<pair<int,int>> Q;
	Q.push(make_pair(0,0));
	G[0]=0;
	int lk,dist;
	while(!Q.empty()){
		lk = Q.front().first;
		dist = Q.front().second;
		Q.pop();
		for(int loop=0;loop<vs[lk].size();loop++){
			if(G[vs[lk][loop]]==-1){
				G[vs[lk][loop]]=dist+1;
				Q.push(make_pair(vs[lk][loop],dist+1));
			}
		}
	}
	for(int loop=0;loop<n;loop++){
		cout << loop+1 << " " << G[loop] << endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int n,g[100][100],f[100],c1[100],c2[100];
void bfs(int now,int far){
  for(int i=0;i<n;i++) if(g[now][i]){
      if(f[i]<far+1){
        c1[i]=1;
        continue;
      }
      f[i]=far+1;
    }
  for(int i=0;i<n;i++) if(g[now][i]&&c1[i]!=1) bfs(i,far+1);
}
int main(){

  int a,b,tmp;
  cin>>n;
  for(int i=0;i<n;i++){
    if(i) f[i]=1000;
    cin>>a>>b;
    for(int j=0;j<b;j++){
      cin>>tmp;
      g[a-1][tmp-1]=1;
    }
  }

  bfs(0,0);

  for(int i=0;i<n;i++) if(f[i]==1000) f[i]=-1;
  for(int i=0;i<n;i++) cout<<i+1<<" "<<f[i]<<endl;

  return 0;

}