Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

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