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"
using namespace std;
typedef long long ll;
// #define int long long

template <class T>
bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
                      << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

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

    int N;
    cin >> N;
    vvi g(N);
    REP(i, N) {
        int u, k;
        cin >> u >> k;
        u--;
        vi v(k);
        REP(j, k) {
            int t;
            cin >> t;
            t--;
            v[j] = t;
        };

        g[u] = v;
    }

    vi dist(N, -1);
    dist[0] = 0;
    queue<int> q;
    q.push(0);

    while (!q.empty())
    {
        int v = q.front();
        q.pop();

        for (int nv : g[v]) {
            if (dist[nv] == -1) q.push(nv);
            if (dist[nv] == -1) dist[nv] = dist[v] + 1;
            else dist[nv] = min(dist[nv], dist[v] + 1);
        }
    }

    REP(i, N) {
        cout << i+1 << " " << dist[i] << endl;
    }
    
    return 0;
}

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

using namespace std;

#define rep(a, b) for(int i = a; i <= b; i++)
#define mst(a) memset(a, 0, sizeof(a))

const int MAX = 1e2 + 10;


bool mtx[MAX][MAX];
int vis[MAX];
int n;

queue<int> q;

void init(void)
{
	while(!q.empty())
		q.pop();
	mst(vis);
	return;
}

bool check(int x, int y)
{
	if(mtx[x][y] && !vis[y])
		return true;
	return false;
}

void GetAns(void)
{
	rep(2, n)
	{
		if(vis[i] == 0)
			vis[i] = -1;
	}
	return;
}

void Bfs(void)
{
	int cnt = 1;
	q.push(1);
	while(!q.empty())
	{
		int cur = q.front();
		q.pop();
		rep(1, n)
		{
			if(check(cur, i))
			{
				q.push(i);
				vis[i] = vis[cur] + 1;
				cnt++;
				if(cnt == n)
					return;
			}
		}
	}
	return;
}

int main()
{
	while(~scanf("%d", &n))
	{
		init();
		rep(1, n)
		{
			int row;
			scanf("%d", &row);
			int num;
			scanf("%d", &num);
			while(num--)
			{
				int tmp;
				scanf("%d", &tmp);
				mtx[row][tmp] = true;
			}
		}
		
		Bfs();

		GetAns();
		
		rep(1, n)
			printf("%d %d\n", i, vis[i]);
	}
	return 0;
}

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

using namespace std;
const static int N = 100;
const static 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++) {
        if (d[i] == INFTY) d[i] = -1;
        cout << i +1 << " " << d[i] << endl;
    }
}

int main() {
    int u, k;
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++){
            int v;
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }
    bfs(0);
    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;
		}
	}
	c[1] = 'g';
	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;
static const int N=100;
static const int INF=(1<<21);

int n,M[N][N];
int d[N];
int v,i;
int u;
void bfs(int s){
  queue<int> q;
  q.push(s);
  for(i=0;i<n;i++)d[i]=INF;
  d[s]=0;
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(v=0;v<n;v++){
      if(d[v]!=INF)continue;
      if(d[v]!=INF)continue;
      d[v]=d[u]+1;
      q.push(v);
    }
  }
  for(i=0;i<n;i++){
    cout << i+1 << " " << ((d[i]==INF) ? (-1) : d[i])<<endl;
  }
}
int main(){
  int u,k,v;
  int i,j;
  cin >> n;
  for(i=0;i<n;i++){
    for(j=0;j<n;j++)M[i][j]=0;
  }
  for(i=0;i<n;i++){
    cin >> u >> k;
    u--;
    for(j=0;j<k;j++){
      cin >> v;
      v--;
      M[u][v]=1;
    }
  }
  bfs(0);

  return 0;
}

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

int a[100][100];
int n;
int D[100];
void bfs(int src) {
	queue<int> Q;
	Q.push(src);
	D[src] = 0;
	while (! Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for (int dst=0; dst<n; ++dst) {
			if (a[cur][dst] == 1 && D[dst] == -1) {
				D[dst] = D[cur]+1;
				Q.push(dst);
			}
		}
	}
}

int main() {
	cin >> n;
	int p,q,r;
	for (int i=0; i<n; ++i) {
		cin >> p >> q;
		D[i] = -1;
		for (int i=0; i<q; ++i) {
			cin >> r;
			a[p-1][r-1] = 1;
		}
	}
	bfs(0);
	for (int i=0; i<n; ++i) {
		cout << i+1 << " " << D[i] << endl;
	}
}

C++: #include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
#define MAX 10010
#define rep(i,p) for(int i=0;i<p;i++)
#define pb push_back
using namespace std;
int n,hoge,foo;
int graph[MAX][MAX] = {0};
int D[MAX] = {-1};
void bfs (int src) {
  queue<int> Q;
  Q.push(src);
  rep(i,MAX) D[i] = -1;
  D[src] = 0;
  while (! Q.empty()) {
    int cur = Q.front();
    Q.pop();
    // cout << "visiting " << cur << " " << D[cur] << endl;
    for (int dst = 1; dst <= n; dst++) {
      if (graph[cur][dst] == 1 && D[dst] == -1) {
        D[dst] = D[cur] + 1;
        Q.push(dst);
      }
    }
  }
}
int main(){
  cin >> n;
  rep(i,n) {
    cin >> hoge >> foo;
    rep(j,foo) {
      cin >> hoge;
      graph[i+1][hoge] = 1;
    }
  }
  bfs(1);
  rep(i,n) cout << i+1 << " " << D[i+1] << endl;
  return 0;
}