Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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

#define REP(i,n) for(int i=0;i<n;++i)
#define SET(a,c) memset(a,c,sizeof(a))

const int MAX_N = 100;
const int INF = 0x3F3F3F3F;

int n, d[MAX_N], G[MAX_N][MAX_N] = {0};

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

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

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

using namespace std;
static const int R =100;
static const int IF = (1<<21);

int num,Mi[R][R],i,x,y;
int D[R];

void BF(int A){
  queue<int> Q;
  Q.push(A);
  for(i = 0;i<num;i++){
    D[i] = IF;
  }
  D[A] = 0;

  while(!Q.empty()){
    x= Q.front();
    Q.pop();

    for(y=0;y<num;y++){
      if(Mi[x][y] == 0){
	continue;
      }
      if(D[y] != IF){
	continue;
	  }
      D[y] = D[x] +1;
      Q.push(y);
    }
  }
  for(i =0; i<num; i++){
    cout << i+1 << " " <<((D[i] ==IF) ? (-1) : D[i]) << endl;
  }
}

  int main(){
    int x,z,y,i,j;

    cin >> num;

    for(i=0;i<num;i++){
      for(j=0;j<num;j++){
	Mi[i][j] = 0;
      }
    }

    for(i = 0;i<num;i++){
      cin >> x >>z;
      x--;

      for(j=0;j<z;j++){
	cin>>y;
	y--;
	Mi[x][y] =1;
      }
    }

    BF(0);

    return 0;
  }

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

int main()
{
    queue<int> Q;
    int n,src=0;
    int a, b, c;
    cin >> n;
    int D[n], in[n][n];
    for (int i = 0; i < n; i++)
    {
        D[i] = -1;
        for (int j = 0; j < n; j++)
            in[i][j] = 0;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> a >> b;
        for (int j = 0; j < b; j++)
        {
            cin >> c;
            in[c - 1][a - 1] = 1;
        }
    }
    Q.push(0);
    D[0] = 0;
    while (!Q.empty)
    {
        int cur = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++)
        {
            if (in[i][cur] == 1 && D[i] == -1) {
                D[i] = D[cur] + 1;
                Q.push(i);
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << " " << D[i] << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int a[101][101],ans=-1,check[101],n;

void calc(int now,int g,int count){
  if(count>ans && ans!=-1) return;
  if(now==g){
    if(ans==-1){
      ans=count;
      return;
    }
    else{
      ans=min(ans,count);
      return;
    }
  }
  if(check[now]>count) check[now]=count;
  else return;
  lp(i,n+1){
    if(a[now][i]==1)
      calc(i,g,count+1);
  }
}

int main(){
  cin>>n;
  lp(i,n){
    int x,y;
    cin>>x>>y;
    lp(j,y){
      int z;
      cin>>z;
      a[x][z]=1;
    }
  }
  lp(i,n){
    lp(j,101){
      check[j]=1000;
    }
    ans=-1;
    calc(1,i+1,0);
    cout<<i+1<<" "<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<queue>
#define Lens 105
using namespace std;
int N;
int A[Lens][Lens];
int d[Lens];
char c[Lens];
queue<int> Q;

int main()
{
	cin >> N;
	Q.push(1);
	for (int i = 1; i <= N; i++)
	{
		int k, n;
		cin >> k >> n;
		c[k] = 'w';
		for (int i = 0; i < n; i++)
		{
			int j;
			cin >> j;
			A[k][j] = 1;
		}
	}
	while (Q.size())
	{
		int i = Q.front();
		Q.pop();
		for(int j=1;j<=N;j++)
		{
			if (A[i][j] == 1 && c[j] == 'w')
			{
				c[j] = 'g';
				d[j] = d[i] + 1;
				Q.push(j);
			}	 
		}
	}
	for (int i = 2; i <= N; i++)
	{
		if (c[i] == 'w')
			d[i] = -1;
	}	
	for (int i = 1; i <= N; i++)
	{
		cout << i << ' ' << d[i] << endl;
	}
	return 0;
}

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

const int INF = 1 << 10;
const int MAX_N = 110;
const int DONE = 2;
const int TOUCHED = 1;
const int NOT_TOUCHED = 0;
int G[MAX_N][MAX_N];
int D[MAX_N];
int V[110];

int main() {
  int n, i, j, u, k, v, d;
  queue<int> q;

  cin >> n;
  for (i = 0; i < n; ++i) {
    D[i] = INF;
  }

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

  D[0] = 0;
  V[0] = TOUCHED;
  q.push(0);
  while (!q.empty()) {
    i = q.front(); q.pop();
    for (j = 0; j < n; ++j) {
      if (G[i][j] && V[j] != DONE) {
        if (V[j] == NOT_TOUCHED) {
          V[j] = TOUCHED;
          q.push(j);
        }
        D[j] = min(D[j], D[i] + 1);
      }
    }
    V[i] = DONE;
  }

  for (i = 0; i < n; ++i) {
    d = D[i] == INF ? -1 : D[i];
    cout << i + 1 << ' ' << d << '\n';
  }

  return 0;
}

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

using i64 = int64_t;

using Graph = vector<vector<int>>;

int main(){
    int n;
    cin >> n;
    Graph graph(n+1, 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;
            graph[u].push_back(v);

        }
    }

    vector<int> mini(n+1, 200);
    queue<pair<int, int>> que;
    mini[1] = 0;
    que.push({1, 0});
    while(!que.empty()){
        auto cur = que.front(); que.pop();
        for(auto to: graph[cur.first]){
            int cost = cur.second + 1;
            if(mini[to] <= cost)continue;
            mini[to] = cost;
            que.push({to, cost});
        }
    }

    for(int i=1;i<=n;++i){
        cout << i << " " << (mini[i] < 200 ? mini[i] : -1) << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using Graph = vector<vector<int>>;
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
const int inf = 1e8;

int main() {
    int n;
    cin >> n;
    Graph g(n);
    rep(i,n) {
        int id,k;
        cin >> id >> k;
        id--;
        rep(j,k) {
            int v;
            cin >> v;
            v--;
            g[id].push_back(v);
        }
    }
    vector<int> d(n);
    rep(i,n) d[i] = inf;
    d[0] = 0;
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        int v = q.front(); q.pop();
        for (int n_v: g[v]) if (chmin(d[n_v],d[v]+1)) q.push(n_v);       
    };
    rep(i,n) if (d[i] == inf) d[i] = -1;
    rep(i,n) cout << i+1 << " " << d[i] << endl;
}