Go: package main

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

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

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

C++: /*
Breadth First Search(https://onlinejudge.u-aizu.ac.jp/#/problems/ALDS1_11_C)
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define rep(n, i) Rep(0, n-1, i)
#define pb(v) push_back(v)
#define ENJYU std::ios::sync_with_stdio(false);std::cin.tie(0);

const int MAX = 128;

//隣接リスト
vector <int> adj[MAX];
//頂点１からの距離
int d[MAX];

void solve(void){
	//init
	ENJYU;
	memset(d, -1, sizeof(d));
	//input
	int N;
	cin >> N;
	rep(N, i) {
		int u, k;
		cin >> u >> k;
		u--;
		rep(k, i) {
			int v;
			cin >> v;
			v--;
			adj[u].pb(v);
		}
	}
	//bfs
	queue <int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		int n = q.front();
		q.pop();
		for (int nv : adj[n]) {
			if (d[nv] >= 0) {
				continue;
			}
			d[nv] = d[n] + 1;
			q.push(nv);
		}
	}

	rep(N, i) cout << i+1 << ' ' << d[i] << endl;

}

int main(void){
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}

C++: #include<algorithm>
#include<bitset>
#include<cassert>
#include<cfloat>
#include<climits>
#include<cmath>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
#include<complex>
#include<list>
#include<cstdio>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)


using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;




const int INF = 1e9 + 7;


/******************************************************************************************/



vector<int> G[105];
int dist[105];


int main() {

	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(10);

	int n;
	cin >> n;

	for (int i = 0; i < 105; i++)
	{
		dist[i] = INF;
	}

	for (int i = 0; i < n; i++)
	{
		int node, out;
		cin >> node >> out;
		for (int j = 0; j < out; j++)
		{
			int c;
			cin >> c;
			G[node].push_back(c);
		}
	}


	queue<int> que;

	que.push(1);
	dist[1] = 0;

	while (!que.empty()) {

		int now = que.front();
		que.pop();

		if (dist[now] == INF)continue;
		for (int nxt : G[now]) {

			if (dist[nxt] > dist[now] + 1) {
				dist[nxt] = dist[now] + 1;
				que.push(nxt);
			}
		}
	}

	for (int i = 1; i <= n; i++)
	{
		if (dist[i] == INF) {
			cout << i << " " << -1 << endl;
		}
		else
		{
			cout << i << " " << dist[i] << endl;
		}
	}
	
	return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];

void bfs(int s) {
	int i, u, v;
	queue<int> q;
	q.push(s);
	for ( i = 0; i < n; i++ ) d[i] = INFTY;
	d[s] = 0;

	while ( !q.empty() ) {
		u = q.frout();
		q.pop();
		for ( 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 ( i = 0; i < n; i++ ) {
			cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
	}
}

int main() {
	int u, k, v, 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 <vector>
#include <queue>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int MAX_N = 100;
vector<int> G[MAX_N];
vector<bool> seen(MAX_N, false);

int main()
{
    int N;
    cin >> N;
    rep(i, N)
    {
        int id, u;
        cin >> id >> u;
        rep(j, u)
        {
            int target;
            cin >> target;
            target--;
            G[i].push_back(target);
        }
    }

    // 最短距離を算出
    queue<int> que;
    que.push(0);

    int distance = 0;
    int d[N];
    fill_n(d, N, -1);
    d[0] = 0;
    seen[0] = true;
    while (!que.empty())
    {
        int origin = que.front();
        int distance = d[origin];
        que.pop();
        for (int target : G[origin])
        {
            if (!seen[target])
            {
                seen[target] = true;
                d[target] = distance + 1;
                que.push(target);
            }
        }
    }

    rep(i, N)
    {
        int id = i + 1;
        cout << id << " " << d[i] << endl;
    }

    return 0;
}

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

const int MAX_N = 100;
int to[MAX_N][MAX_N];
int ans[MAX_N];
bool visited[MAX_N];

int main() {
  int N;
  cin >> N;
  for (int i=0; i<N; i++) {
    for (int j=0; j<N; j++) {
      to[i][j] = -1;
    }
    ans[i] = -1;
    visited[i] = false;
  }
  for (int i=0; i<N; i++) {
    int tmp, n;
    cin >> tmp >> n;
    for (int j=0; j<n; j++) {
      int t;
      cin >> t; t--;
      to[i][j] = t;
    }
  }

  queue<pair<int, int> > que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    pair<int, int> tmp = que.front(); que.pop();
    int x = tmp.first, depth = tmp.second;
    if (ans[x] == -1) {
      ans[x] = depth;
    } else {
      ans[x] = min(ans[x], depth);
    }
    for (int i=0; to[x][i] != -1; i++) {
      que.push(make_pair(to[x][i], depth + 1));
    }
  }

  for (int i=0; i<N; i++) {
    cout << i+1 << " " << ans[i] << endl;
  }
  return 0;
}