Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

C++: 
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define veci vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vec>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))


using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
//template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
//template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
    for(T& x:vec) is >> x;
    return is;
}
const ll INF=1e9+7;

ll m[105][105];
ll d[105];
ll n;


void bfs(ll s){
    queue<ll> q;
    q.push(s);
    rep(i,n){
        d[i]=INF;
    }
    d[s]=0;
    while(!q.empty()){
        ll u=q.front();
        q.pop();
        rep(v,n){
            if(m[u][v]==0) continue;
            if(d[v]!=INF) continue;
            d[v]=d[u]+1;
            q.push(v);
        }
    }
    rep(u,n){
        cout << u+1 << " " << (d[u]==INF?-1:d[u]) << endl;
    }
}

int main()
{
    cin >> n;
    rep(u,n){
        rep(v,n){
            m[u][v]=0;
        }
    }
    ll u,k,v;
    rep(i,n){
        cin >> u >> k;
        u--;
        rep(j,k){
            cin >> v;
            v--;
            m[u][v]=1;
        }
    }
    bfs(0);
    return 0;
}

C++: /**
 * Bredth first search.
 */

#include <iostream>
using namespace std;
const int N = 101;
int adj[N][N] = {0};
int d[N]={0};

void bfs(int n, int dd) {
	int k = adj[n][0];
	d[n] = dd++;
	for (int j=1;j<=k;j++) {
		bfs(adj[n][j], dd);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i=1;i<=n;i++){
		int j, k;
		cin >> j >> k;
		adj[j][0]=k;
		for (int m=1;m<=k;m++) {
			int a;
			cin >> a;
			adj[j][m]=a;
		}
	}

	bfs(1, 0);

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

	return 0;
}

C++: #include<iostream>
#define INF 1000000
using namespace std;

int que[100000];
int head,tail,leng=100000;
 
int isEmpty(){
  return head==tail;
}
 
void enqueue(int x){
  que[tail]=x;
  if(tail+1==leng) tail=0;
  else tail++;
}
 
int dequeue(){
  int x;
  x=que[head];
  if(head+1==leng) head=0;
  else head++;
  return x;
}
 

int a[101][101];
int d[101];
int n;

void bfs(int s){
int t;
  for(int i=1;i<=n;i++)d[i]=INF;
  d[s]=0;
   while(!isEmpty()){
    t=dequeue();
    for(int i=1;i<=n;i++){
      if(d[i]!=INF || a[t][i]!=1) continue;
       enqueue(i),d[i]=d[t]+1;
    }
  }
}

int main(){
  int top,m,s;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>top>>m;
    for(int j=0;j<m;j++)
      cin>>s,a[top][s]=1;
     } 
  enqueue(1);
   bfs(1);
   for(int i=1;i<=n;i++){
     if(d[i]==INF) d[i]=-1;
     cout<<i<<" "<<d[i]<<endl;
   }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for(ll i = (a); i > (b); --i)
#define REP(i, n) for(ll i = 0; i < (n); ++i)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define mp make_pair
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CINT(...)                                                              \
    int __VA_ARGS__;                                                           \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    ll __VA_ARGS__;                                                            \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"

typedef long long ll;
typedef unsigned long long ul;
typedef vector<int> V;
typedef vector<ll> VL;
typedef pair<ll, ll> PL;
typedef priority_queue<ll> PQ;
typedef priority_queue<ll, V, greater<ll>> PQG;

const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18;

ll N, M;
vector<VL> G;
vector<bool> went;
VL d;

signed main() {
    cin >> N;
    G.resize(N);
    d.resize(N, LINF);
    REP(i, N) {
        LCIN(u, k);
        REP(i, k) { G[u - 1].emplace_back(IN() - 1); }
    }
    queue<ll> q;
    q.emplace(0);
    d[0] = 0;
    while(!q.empty()) {
        ll now = q.front();
        q.pop();
        FOREACH(to, G[now]) {
            if(d[to] > d[now] + 1) {
                d[to] = d[now] + 1;
                q.emplace(to);
            }
        }
    }
    REP(i, N) { cout << i + 1 << " " << (d[i] == LINF ? -1 : d[i]) << "\n"; }
    return 0;
}