Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

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

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cctype>
#include <bits/stdc++.h>
#include <queue>

using namespace std;
// for loop
#define F(i, L, R) for(long i = L; i < R; i++)
#define FE(i, L, R) for(long i = L; i <= R; i++)
// for (reversed)
#define FR(i, L, R) for(long i = L; i > R; i--)
#define FRE(i, L, R) for(long i = L; i >= R; i--)

// data types
#define Int long
#define ul unsigned long
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned shoft

// vectors
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define ALL(c) (c).begin(),(c).end()
#define last(vec) vec.size()-1
#define lastEle(vec) vec[last(vec)]
#define pb push_back
#define InputV(a, n) F(i,0,n){long l; cin >> l; a.pb(l);}

// debug
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a, L, R) FE(i, L, R) cout << a[i] << (i==R?'\n':' ')
#define printAF(a, L) printA(a, 0, L-1)
#define printV(a) printA(a, 0, a.size()-1)

// utils
#define char2int(c) (c-'0')
#define bit(x, i) (x&(1<<i))
#define space ' '
#define PI 3.14159265358979323

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

void printM(vector<vector<int> > &vec, long height, long width){
    F(i, 0, height){
        F(j, 0, width){
            cout << vec[i][j] << space;
        }
        cout << endl;
    }
}

vector<int> seen;
vector<int> done;
vector<int> cost;
int timestamp;

// Graph: グラフを表した隣接リスト
// v: 何番目のノードを訪問するのか
void dfs(vector<vector<int> > &Graph, int v){
    timestamp++;
    seen[v] = timestamp; //訪問済みにする

    // 隣接するノードを再帰的に訪問
    for(int next_v = 0; next_v < Graph[v].size();next_v++) {
        if(seen[Graph[v][next_v]] != -1) continue;
        dfs(Graph, Graph[v][next_v]);
    }
    // 出るときの時間が増える
    timestamp++;
    done[v] = timestamp; // 隣接ノードを全て調べ終えた
}

queue<int> que;
vector<long> dist;
// v: 何番目の頂点を訪問しているのか
void bfd(vector<vector<int> > &Graph, int v){
    done[v] = timestamp;

    for(int next_v = 0; next_v < Graph[v].size();next_v++){
        if(seen[Graph[v][next_v]] == -1){
            dist[Graph[v][next_v]] = dist[v] + 1;
            seen[Graph[v][next_v]] = timestamp;
            que.push(Graph[v][next_v]);
        }else{
            dist[Graph[v][next_v]] = min(dist[Graph[v][next_v]], dist[v] + 1);
        }
    }
}

#define TST 0
int main() {
#if TST==1
    ifstream in ("input.txt");
    cin.rdbuf(in.rdbuf());
#endif
    int n;
    cin >> n;
    vvi Graph;
    Graph.resize(n);
    F(i, 0, n){
        int id;
        cin >> id;
        int k;
        cin >> k;
        vi tmp;
        tmp.resize(k);
        F(j, 0, k){
           cin >> tmp[j]; 
           tmp[j]--;
        }
        Graph[i] = tmp;
    }
    seen.assign(n, -1);
    done.assign(n, -1);
    dist.assign(n, -1);
    que.push(0);
    seen[0] = 0;
    dist[0] = 0;
    while(!que.empty()){
       timestamp++;
       int front = que.front();
       bfd(Graph, front);
       que.pop();
    }

    F(i, 0, n){
        cout << i+1 << space << dist[i] << endl;;
    }

#if TST==1
#endif
}

C++: #include <bits/stdc++.h>
using namespace std;

#define INFTY 100000

void bfs(int s, vector<vector<int>> &M) {
    int n = M.size();
    queue<int> q;
    q.push(s);

    int d[n];
    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++) {
        cout << i + 1 << " ";
        if (d[i] == INFTY)
            cout << -1;
        else
        {
            cout << d[i];
        }
        cout << endl;
    }
}

int main() {
    int n;
    cin >> n;

    vector<vector<int>> M(n, vector<int>(n, 0));
    int u, k, v;
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++)
        {
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0, M);
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
//original idea: stupid wanna check the node with many bfs search 
using namespace std;
#define N 105 // number of nodes
vector<int> neighbors[N]; //start from 1
queue<int> q;
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C bfs
int dist[N];
int vis[N];
int res = 0;
void bfs(int d){
	if(q.empty()) return;
	//until q is empty
	while(!q.empty()){
		int temp = q.front();
		q.pop();
		if(dist[temp]!=-1){
			if(d<dist[temp]) dist[temp] = d;
			else {dist[temp] = dist[temp];}
		}else {
			dist[temp] = d;
		}
		for(int i = 0; i<neighbors[temp].size(); i++){
			d = dist[temp] +1;//next lever
		if(dist[neighbors[temp].at(i)] == -1){//not visited
			q.push(neighbors[temp].at(i));
			dist[neighbors[temp].at(i)] = d;
			}
		}
		
	}
	
}

int main()
{
    int n;
    scanf("%d",&n);
    memset(dist, -1, sizeof(dist));
    memset(vis, -1, sizeof(dist));
    for(int i = 0; i<n; i++){
        int node;
        scanf("%d",&node);
        int num;
        scanf("%d",&num);
        for(int j = 0; j<num; j++){
        	int temp;
        	scanf("%d",&temp);
        	neighbors[node].push_back(temp);
        	neighbors[temp].push_back(node);
		}
    }
	q.push(1);//begin from 1
    bfs(0);
    for(int i = 1; i<=n; i++){
    	printf("%d %d\n",i, dist[i]);
	}
    return 0;
}

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

 queue<int> q;
int a[101][101];
int d[101];
int n;

void bfs(int s){
  int t;
  for(int i=1;i<=n;i++)d[i]=INF;
  d[s]=0;
  while(!q.empty()){
    t=q.front(),q.pop();
    for(int i=1;i<=n;i++){
      if(d[i]!=INF || a[t][i]!=1) continue;
       q.push(i),d[i]=d[t]+1;
    }
  }
}

int main(){
  int top,m,s;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>top>>m;
    for(int j=0;j<m;j++)
      cin>>s,a[top][s]=1;
     } 
  q.push(1);
   bfs(1);
  for(int i=1;i<=n;i++) cout<<i<<" "<<d[i]<<endl;
    return 0;
}