Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
)

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func print() {
	for i:=0; i<N; i++ {
		fmt.Printf("%d %d\n", i+1, d[i])
	}
}

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

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

#define __sel(_1, _2, x, ...) x

#define repn(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, ...) __sel(__VA_ARGS__, repr, repn)(i, __VA_ARGS__)

#define rrepn(i, n) for (ll i = (n - 1); i >= 0; i--)
#define rrepr(i, a, b) for (ll i = (b - 1); i >= (a); i--)
#define rrep(i, ...) __sel(__VA_ARGS__, rrepr, rrepn)(i, __VA_ARGS__)

#define umax(m, x) (m = max(m, x))
#define umin(m, x) (m = min(m, x))
#define rng(v) v.begin(), v.end()
#define rrng(v) v.rbegin(), v.rend()
#define bit(x, n) ((x) >> (n) & 1)

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define pt(x) cout << (x) << endl;
#define shv(v) for (auto __t1 : v) cerr << __t1 << " "; cerr << endl;
#define db(x) cerr << #x << ": " << (x) << endl;
#define dbv(v) cerr << #v << ": "; shv(v);
#define dbm(m) cerr << #m << ":" << endl; for (auto __t2 : m) { shv(__t2); }

using namespace std;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vd = vector<double>;
using vld = vector<ld>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vpii = vector<pii>;
using vpll = vector<pll>;

using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
using vvld = vector<vector<ld>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;

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

inline int gi() { int x; cin >> x; return x; }
inline ll gl() { int x; cin >> x; return x; }
inline char gc() { char x; cin >> x; return x; }
inline string gs() { string x; cin >> x; return x; }

inline vi gvi(int n) { vi v(n); rep(i, n) cin >> v[i]; return v; }
inline vl gvl(int n) { vl v(n); rep(i, n) cin >> v[i]; return v; }
inline vc gvc(int n) { vc v(n); rep(i, n) cin >> v[i]; return v; }
inline vs gvs(int n) { vs v(n); rep(i, n) cin >> v[i]; return v; }

inline vvi gvvi(int m, int n) { vvi v(n, vi(m)); rep(i, n) rep(j, m) cin >> v[i][j]; return v; }
inline vvl gvvl(int m, int n) { vvl v(n, vl(m)); rep(i, n) rep(j, m) cin >> v[i][j]; return v; }
inline vvc gvvc(int m, int n) { vvc v(n, vc(m)); rep(i, n) rep(j, m) cin >> v[i][j]; return v; }
inline vvs gvvs(int m, int n) { vvs v(n, vs(m)); rep(i, n) rep(j, m) cin >> v[i][j]; return v; }

inline void yn(bool f) { cout << (f ? "Yes" : "No") << endl; }


int main() {
    int n = gi();
    vvi g(n);
    rep(i, n) {
        int u = gi() - 1;
        int k = gi();
        g[u] = gvi(k);
    }

    queue<int> q;
    vi dist(n, INF);
    dist[0] = 0;
    q.push(0);

    while (!q.empty()) {
        auto t = q.front(); q.pop();
        for (auto tt : g[t]) {
            if (dist[tt - 1] != INF) continue;
            umin(dist[tt - 1], dist[t] + 1);
            q.push(tt - 1);
        }
    }

    rep(i, dist.size()) {
        cout << i + 1 << " ";
        if (dist[i] == INF)
            cout << -1 << endl;
        else
            cout << dist[i] << endl;
    }

    return 0;
}

C++: #include <stdio.h>
#define GRAY  2 
#define WHITE 1
#define BLACK 3 
#define INF -1
int n,color[110],d[110],Q[110],adj[110][110];
int head=0,tail=0;
int judge(){
  if(tail==head) return 1;

  return 0;
}

void enQ(int s){
  Q[tail]=s;
  if(tail==110)tail=0;
  else tail++;
}
int deQ(){
  int a;
  a=Q[head];
  if(head==100)head==0;
  else head++;

  return a;
}

void bfs(int s){
  int i,j,u;
  for(i=2;i<=n;i++){
    color[i]=WHITE;
    d[i]=INF;
  }

  color[s]=GRAY;
  d[s]=0;
 
  enQ(s);

  while(judge() != 1){
 
    u = deQ();
 
    for( j = 1; j <= n; j++ ){
      if(color[j]==WHITE&&adj[u][j]==1){
 
	color[j] = GRAY;
 
	d[j] = d[u] + 1;
 
	enQ(j);
      }
    }
    color[u]=BLACK;
  }

}


int main(){

  int u,k,v,i,j;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d%d",&u,&k);
    for(j=0;j<k;j++){
      scanf("%d",&v);
      adj[u][v]=1;
    }
  } 
  bfs(1);

  for(i=1;i<=n;i++){
    printf("%d %d\n",i,d[i]);
  }
  return 0;



}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;
static const int INF = 1e9;
static const int SIZE = 101;
typedef pair<int, int> P;

int d[SIZE];
vector<int> G[SIZE];

void solve()
{
	int n;
	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].push_back(v);
		}
	}
	for (int i = 0; i < SIZE; ++i)
	{
		d[i] = INF;
	}
	d[1] = 0;
	queue<P> que;
	que.push(P(0, 1));
	while (!que.empty())
	{
		P p = que.front();
		que.pop();
		int v = p.second;
		for (int i = 0; i < G[v].size(); ++i)
		{
			int to = G[v][i];
			if (d[to] > p.first + 1)
			{
				d[to] = p.first + 1;
				que.push(P(d[to], to));
			}
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		if (d[i] == INF)
		{
			cout << i << " " << -1 << endl;
		}
		else
		{
			cout << i << " " << d[i] << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

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

int main(){
  int n;
  cin >> n;
  
  vector<vector<int>> g(n,vector<int>());

  for (int i = 0; i < n; i++)
  {
    int u,k;
    cin >> u >> k;

    for (int j = 0; j < k; j++)
    {
      int v;
      cin >> v;
      v--;
      g[i].push_back(v);
      // g[v].push_back(i);
    }
  }

  vector<int> dist(n,-1);
  dist[0] = 0;
  queue<int> q;
  q.push(0);
  while(!q.empty()){
    int node = q.front();
    q.pop();
    for(int next:g[node]){
      if(dist[next]!=-1) continue;
      dist[next] = dist[node]+1;
      q.push(next);
    }
  }
  for (int i = 0; i < n; i++)
  {
    cout << i+1 << " " << dist[i] << endl;
  }  
}