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<queue>

using namespace std;
static const int N = 100;
static const 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++ ) {
    cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
  }
}

int main() {
  int u, k, v;

  cin >> n;
  for ( int i = 0; i < n; i++ ){
    for ( int j = 0; j < n; j++ ) M[i][j] = 0;
  }

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

  return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n,M[N][N];
ind 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++ ){
    cout << i + 1 << " " <<((d[i] == INFTY) ? ( -1 ) : d[i]) << endl;
  }
}

int main( void ){
  int u,k,v;
  cin >> n:
  for( int i = 0 ; i < n ; i++ ){
    for( int j = 0 ; j < n ; j++ ){
      M[i][j] = 0;
    }
  }

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

  return 0;
}

C++: #include<stdio.h>
#include<queue>

using namespace std;
static const int N=100;
static const int INFTY=(1<<21);

int n,M[N][N];
int d[N];

void bfs(int s){
  int i,v,u;
  queue<int> q;
  q.push(s);
  for(i=0;i<n;i++){
    d[i]=INFTY;
  }
  d[s]=0;
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(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(i=0;i<n;i++){
    printf("%d %d\n",i+1,((d[i]==INFTY)?(-1) : d[i]));
  }
}

int main(){
  int i,j,u,k,v;

  scanf("%d",&n);

  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      M[i][j]=0;
    }
  }

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

  bfs(0);

  return 0;
}

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

typedef pair< int,int > P;
int N;
vector< vector< int > > Edge;
vector< int > Csum;

void bfs(){
  Csum.assign(N+1,-1);
  queue< P > que;
  que.push(P(0,0));
  Csum[0] = 0;
  while(!que.empty()){
    int now = que.front().second;
    int cost = que.front().first;
    que.pop();
    for(int i=0;i<Edge[now].size();i++){
      if(Csum[Edge[now][i]] != -1) continue;
      Csum[Edge[now][i]] = cost+1;
      que.push(P(cost+1,Edge[now][i]));
    }
  }
}

int main(){
  cin >> N;
  Edge.resize(N+1);
  for(int i=0;i<N;i++){
    int u,k; cin >> u >> k;
    for(int j=0;j<k;j++){
      int e; cin >> e; e--;
      Edge[i].push_back(e);
    }
  }
  bfs();
  for(int i=0;i<N;i++){
    cout << i+1 << " " << Csum[i] << endl;
  }
}

C++: #include <iostream>
#include <queue>
using namespace std;
static const int N=100;
static const int INFTY=(1<<21);

int M[N][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++){
		cout<<i+1<<" "<<((d[i]==INFTY)?(-1):d[i])<<endl;
	}
}

int main(){
	
	int k,u,v;
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++)
			M[i][j]=0;
	}
	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);
	
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <stdio.h>
#define REP(i, n) for(int i = 0;i < n;i++)

using namespace std;

static const int N = 100;
static const int INFTY = (1 << 21);

int n, M[N][N];
int d[N];

void bfs(int s)
{
	queue<int>	q;
	q.push(s);
	REP(i, n)
		d[i] = INFTY;
	d[s] = 0;
	int u;

	while (!q.empty())
	{
		u = q.front();
		q.pop();
		REP(v, n)
		{
			if (M[u][v] == 0)
				continue;
			if (d[v] != INFTY)
				continue;
			d[v] = d[u] + 1;
			q.push(v);
		}
	}
	REP(i, n)
		cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
}

int main()
{
	int u, k, v;

	cin >> n;

	REP(i, n)
	{
		REP(j, n)
			M[n][n] = 0;
	}

	REP(i, n)
	{
		cin >> u >> k;
		u--;
		REP(j, k)
		{
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}



	bfs(0);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N]; // 頂点0からの距離

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] && d[v] == INFTY) {
                // 未訪問
                q.push(v);
                d[v] = d[u] + 1;
            }
        }
    }

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

int main() {
    int u, k, v;
    cin >> n;
    // 初期化
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            M[i][j] = 0;
        }
    }

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

    return 0;
}