Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

C++: #include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T& a, const T& b)
{
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b)
{
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(x) (x).begin(), (x).end() //sortなどの引数を省略したい
#define F first
#define S second

typedef long long ll;
const ll MOD = 1e9 + 7;
const int inf = 1000000;
const int minf = -1000000;
const ll lnf = 1e12;
const ll mlnf = -1e12;

// Graph problem
using Graph = vector<vector<int>>;
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };

// coding start

int main()
{
    int N;
    cin >> N;
    Graph G(N);
    rep(i, N)
    {
        int hoge, k;
        cin >> hoge >> k;
        rep(j, k)
        {
            int tmp;
            cin >> tmp;
            tmp--;
            G[i].push_back(tmp);
        }
    }
    vector<int> dist(N, -1);
    dist[0] = 0;
    queue<int> que;
    que.push(0);
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (auto nv : G[v]) {
            if (dist[nv] != -1)
                continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    rep(i, N) printf("%d %d\n", i + 1, dist[i]);
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n);
  REP(i, n) {
    int u, k;
    cin >> u >> k, u--;
    REP(j, k) {
      int v;
      cin >> v, v--;
      G[u].push_back(v);
    }
  }
  vector<bool> ed(n, false);
  vector<int> dis(n, -1);
  queue<P> que{};
  ed[0] = 1;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int id = p.first;
    int d = p.second;
    dis[id] = d;
    REP(i, G[id].size()) {
      if (ed[G[id][i]] == true) continue;
      ed[G[id][i]] = true;
      que.push(P(G[id][i], d + 1));
    }
  }
  REP(i, n) { cout << i + 1 << " " << dis[i] << endl; }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <map>
#include <cmath>
#include <queue>

using namespace std;

static const int N = 100;

int main() {

    int graph[N][N] = {0};
    int distance[N] = {INT_MIN};
    bool alreadySearched[N] = {false};

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


    queue<int> Q;
    Q.push(0);
    distance[0] = 0;
    alreadySearched[0] = true;

    while(!Q.empty()){
        int u = Q.front();
        for(int i=0;i<n;i++){
            if(!alreadySearched[i] && graph[u][i] == 1){
                Q.push(i);
                alreadySearched[i] = true;
                distance[i] = distance[u] + 1;
            }
        }
        Q.pop();
    }

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

    return 0;
}

C++: /*
問題の名称：Graph I - Breadth Forst Search
問題のURL：http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=jp
所要時間：50分程度
学んだこと：幅優先探索
着席位置：中央　中央
*/
#include <iostream>
#include <queue>
using namespace std;
const int INF = -1;

int main()
{
	int n;
	cin >> n;
	vector<int> edges[n];
	vector<int> d(n);

	d[0] = 0;

	for (int i = 1; i < n; ++i)
	{
		d[i] = INF;
	}

	int u,k,v;

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

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

	queue<int> Q;
	Q.push(0);
	int temp,next;
	while(!Q.empty())
	{
		temp = Q.front();
		Q.pop();
		for (int i = 0; i < edges[temp].size(); ++i)
		{
			next = edges[temp][i];
			if (d[next] == INF)
			{
				Q.push(next);
				d[next] = d[temp] + 1;
			}
		}
	}

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