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++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_A
// Graph I - Breadth First Search

#define SUBMIT

#include <utility>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <limits>
#include <numeric>
#include <functional>
#include <set>
#include <cmath>
#include <cstring>
#include <climits>
#include <memory>
#include <queue>

using namespace std;
using ui64 = unsigned long long;
using i64 = long long;

const ui64 MAX_N = 100;

enum class color_type {
    white,
    gray
};

const int NIL = -1;

int adj[MAX_N][MAX_N];
color_type color[MAX_N];
int dist[MAX_N];
int n;
queue<int> q;

void bfs(int u) {
    q.push(u);

    while (!q.empty()) {
        u = q.front();
        q.pop();

        for (int v = 0; v < n; ++v) {
            if (adj[u][v] == 1 && color[v] == color_type::white) {
                q.push(v);
                color[v] = color_type::gray;
                dist[v] = dist[u] + 1;
            }
        }
    }
}

void start_bfs() {
    while (!q.empty()) q.pop();

    dist[0] = 0;
    color[0] = color_type::gray;
    bfs(0);
}

int main() {
    memset(adj, 0, sizeof(adj));
    memset(dist, NIL, sizeof(dist));
    cin >> n;

    int u, degree;
    for (int i = 0; i < n; ++i) {
        cin >> u >> degree;
        u--; // 0-origin

        int v;
        for (int j = 0; j < degree; ++j) {
            cin >> v;
            v--; //0-origin
            adj[u][v] = 1;
        }
    }

    start_bfs();
    for (int v = 0; v < n; ++v) cout << v + 1 << " " << dist[v] << endl;
    return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef stack<int> SI;
typedef queue<int> QI;
typedef pair<int,int> PII;
typedef long long LL;

#define d(x)  cout<<#x<<" = "<<(x)<<endl;
#define c cout<<"#"<<endl;
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define REACH(i,c) for(typeof((c).rbegin()) i=(c).rbegin(); i!=(c).rend(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c,n) sort(c,c+n)
#define VSORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define DFOR(i,b,a) for(int i=(b)-1;i>=(a);--i)
#define DREP(i,n) DFOR(i,n,0)
#define INF 2000000000

const double EPS = 1e-10;
const double PI  = acos(-1.0);
////////////////////////////////////////////////
int n,a[101][101],d[101],p[101];
QI q;
int main()
{
	cin>>n;
	FOR(i,1,n+1)
	{
		int k,m;cin>>k>>m;
		a[k][0]=m;
		FOR(j,1,m+1)
		{	
			int x;cin>>x;
			a[k][j]=x;
		}
	}
	REP(i,101)p[i]=1;
	for(q.push(1);SZ(q)!=0;)
	{
		int u=q.front();
		q.pop();
		for(;p[u]<=a[u][0];)
		{
			int v=a[u][p[u]];p[u]++;
			if(v!=1&&d[v]==0)
			{
				d[v]=d[u]+1;
				q.push(v);
			}
		}
	}
	FOR(i,1,n+1)
	{
		cout<<i<<" "<<d[i]<<endl;
	}
	return 0;
}

C++: #include <queue>
#include <map>
#include <cstdio>

using namespace std;

int main(){
	int t=0,T,i,j,k;
	map<int,vector<int> >a;
	for(scanf("%d",&T); t<T; t++)for(scanf("%d%d",&i,&k); k--; a[i].push_back(j))scanf("%d",&j);
	map<int,int>m;
	queue<int>q;
	for(q.push(1),m[1]=1;!q.empty();){
		k = q.front();
		q.pop();
		for (i = 0; i < a[k].size(); i++){
		    if(!m[a[k][i]])m[a[k][i]]=m[k]+1,q.push(a[k][i]);
		}
	}
	for(i=1;i<=T;i++){
	    printf("%d %d\n",i,m[i]-1);
	}
}

C++: #include<iostream>
#include<queue>
using namespace std;
int map[101][101];
int all = 0;
int search_2[101];
int n;
void bfs(int k, int m) {
	search_2[k] = m;
	queue<int>q;
	for (int i = 2; i <= n; i++) {
		if (map[k][i] && (search_2[i] == 0 || search_2[i] > m + 1)) {
			q.push(i);
		}
	}
	while (!q.empty())
	{
		bfs(q.front(), m + 1);
		q.pop();
	}
}
int main(void) {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		int m;
		cin >> m;
		for (int j = 0; j < m; j++) {
			int b;
			cin >> b;
			map[a][b] = 1;
		}
	}
	bfs(1, 0);
	cout << 1 << " " << 0 << endl;
	for (int i = 2; i <= n; i++) {
		cout << i << " ";
		if (search_2[i] == 0) cout << -1 << endl;
		else cout << search_2[i] << endl;
	}
}

C++: ///
// File:  alds1_11_c.cpp
// Author: ymiyamoto
//
// Created on Wed Oct 18 00:49:05 2017
//

#include <cstdint>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
static uint32_t graph[100][100];
static vector<int32_t> d(100, -1);
static queue<uint32_t> q;

static void bfs(uint32_t n)
{
  q.push(0);
  d[0] = 0;

  while (!q.empty()) {
    uint32_t index = q.front();
    q.pop();

    for (uint32_t i = 0; i < n; i++) {
      if (d[i] == -1 && graph[index][i] == 1) {
        q.push(i);
        d[i] = d[index] + 1;
      }
    }
  }
}

int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    uint32_t id, k;
    cin >> id >> k;
    for (uint32_t j = 0; j < k; j++) {
      uint32_t val;
      cin >> val;
      graph[id - 1][val - 1] = 1;
    }
  }

  bfs(n);

  for (uint32_t i = 0; i < n; i++) {
    if (i != 0) cout << " ";
    cout << d[i];
  }
  cout << endl;

  return 0;
}