Go: package main

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

type Queue []int

func (q *Queue) push(v int) {
	*q = append(*q, v)
}

func (q *Queue) pop() int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]int, 0))
	queue.push(1)
	paths[1] = 1

	for len(queue) > 0 {
		u := queue.pop()
		d := paths[u] + 1
		for _, v := range graph[u] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(v)
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const int MOD = 1000000007;

template <typename T>
void print(const T &v);

vector<int> dist(105, INF);
vector<int> grid[105];
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            grid[u].push_back(v);
        }
    }
    queue<int> qe;
    qe.push(1);
    dist[1] = 0;

    while (!qe.empty())
    {
        int now = qe.front();
        qe.pop();
        for (int next : grid[now])
        {
            if (dist[next] <= dist[now] + 1)
            {
                continue;
            }
            if (dist[next] != INF)
            {
                dist[next] = dist[now] + 1;
                continue;
            }
            else
            {
                dist[next] = dist[now] + 1;
            }
            qe.push(next);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (dist[i] != INF)
        {
            cout << i << ' ' << dist[i] << endl;
        }
        else
        {
            cout << i << ' ' << -1 << endl;
        }
    }
    return 0;
}

// Use For Debug
template <typename T>
void print(T const &v)
{
    for (int i = 0; i < v.size(); i++)
    {
        if (i)
            cout << ' ';
        cout << v[i];
    }
    cout << endl;
}

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;
}