Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

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

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<tuple>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<deque>
#include<cmath>
#include<stack>
#define rep(i,n) for(int i = 0;i<n;i++)
#define _GLIBCXX_DEBUG
using ll = long long; 
using namespace std;

static const int N = 100;
static const int INF = (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] = INF;
    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] != INF) continue;
            d[v] = d[u]+1;
            q.push(v);
        }
    }
    for(int i = 0;i < n; i++) {
        cout << i+1 << " " << ( (d[i] == INF) ? (-1):d[i]) << endl;
    }}

    int main() {
        int u,k,v;
        rep(i,n) {
            rep(j,n) {
                M[i][j] = 0;
            }
        }
        cin >> n;
        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<vector>
#include<queue>
#define WHITE 0 //未発見
#define GRAY 1 //発見
#define BLACK 2 //探索済み
#define NIL -1
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef vector< vector<int> > table;

void bfs(table& m,int s)
{
    vector<int> color(m.size(),WHITE);
    vector<int> dist(m.size(),NIL);
    queue<int> q;
    q.push(s);
    color[s]=GRAY;
    dist[s]=0;

    while (!q.empty())
    {
        int u=q.front();q.pop();
        rep(v,m.size())
        {
            if(dist[v]!=NIL)continue;
            if(m[u][v]==1 && color[v]==WHITE)
            {
                dist[v]=dist[u]+1;
                color[v]=GRAY;
                q.push(v);
            }
        }
        color[u]=BLACK;
    }
    
    rep(i,m.size()) cout<<i+1<<" "<<dist[i]<<endl;
}

int main()
{
    int n;
    cin>>n;
    table m(n,vector<int>(n,0));
    rep(i,n)
    {
        int u,k;
        cin>>u>>k;
        rep(j,k)
        {
            int v;
            cin>>v;
            m[u-1][v-1]=1;
        }
    }

    bfs(m,0);

    return 0;
}

C++: #include <queue>
#include <map>
#include <cstdio>
using namespace std;
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 <unordered_map>
#include <queue>
#include <vector>

using namespace std;
int main(){
  int u, i, id, k, j;
  cin >> u;
  unordered_map<int, vector<int> > mymap;
  queue<vector<int> > myqueue;
  for (i = 0; i < u; ++i){
    cin >> id >> k;
    vector<int> points;
    for (j = 0; j < k; ++j){
      int a;
      cin >> a;
      points.push_back(a);
    }
    mymap[id] = points;
    if(id == 1){
      myqueue.push(points);
    }
  }
  
  bool visited[u+1];
  for (j = 1; j <= u; ++j){
    visited[j] = false;
  }
  visited[1] = true;

  int dist[u+1];
  for (j = 1; j <= u; ++j){
    dist[j] = -1;
  }
  dist[1] = 0;
  int d = 1;

  while(!myqueue.empty()){
    vector<int> combined;
    for (i = 0; i < myqueue.front().size(); ++i){
      if(!visited[myqueue.front()[i]]){
        dist[myqueue.front()[i]] = d;
        visited[myqueue.front()[i]] = true;
        if(combined.empty()){
          combined = mymap[myqueue.front()[i]];
        }else{
          combined.insert(combined.end(), mymap[myqueue.front()[i]].begin(), mymap[myqueue.front()[i]].end());
        }
      }
    }
    if(!combined.empty()){
      myqueue.push(combined);
    }
    myqueue.pop();
    d++;
  }

  for (j = 1; j <= u; ++j){
    cout << j << " " << dist[j] << endl;
  }

}

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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define debug3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define debugB(x, y) cerr<<#x<<": "<<bitset<y>(x) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};

template<typename T>
void printA(vector<T> &printArray, char between = ' ') {
   int paSize = printArray.size();
   for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between;}
   if (between != '\n') {cerr << endl;}
}

// ------------------------------------------------------------------------------------------

int N;
int dist[111];
vector<vector<int>> G;

int main() {
   cin >> N;
   G = vector<vector<int>>(N);
   rep (i, N) {
      int u, k; cin >> u >> k; u--;
      rep (i, k) {
         int a; cin >> a; a--;
         G[u].push_back(a);
      }
   }

   rep (i, N) dist[i] = -1;

   queue<int> que;
   que.push(0);
   dist[0] = 0;
   while (que.size()) {
      int cur = que.front(); que.pop();
      for (int x : G[cur]) {
         if (dist[x] == -1) {
            dist[x] = dist[cur] + 1;
            que.push(x);
         }
      }
   }

   rep (i, N) {
      printf("%d %d\n", i+1, dist[i]);
   }


   return 0;
}

C++: #if 0

#endif

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int N;
int GM[110][110];

int D[110];
void bfs(int src) {
  queue<int> Q;
  Q.push(src);
  D[src] = 0;
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    for (int dst = 1; dst <= N; dst++) {
      if (GM[cur][dst] == 1 && !(D[dst] > 0)) {
        D[dst] = D[cur] + 1;
        Q.push(dst);
      }
    }
  }
}

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++)
      GM[i][j] = 0;
  }
  int u, k;
  for (int i = 1; i <= N; i++) {
    cin >> u >> k;
    int t;
    for (int j = 0; j < k; j++) {
      cin >> t;
      GM[i][t] = 1;
    }
  }
  bfs(1);
  cout << "1 0" << endl;
  for (int i = 2; i <= N; i++) {
    cout << i << ' ' << (D[i] > 0 ? D[i] : -1) << endl;
  }
}