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++: /**
 * @Author: yujirotakahashi
 * @Date:   2020-05-17T20:21:41+09:00
 * @Last modified by:   yujirotakahashi
 * @Last modified time: 2020-05-24T01:58:29+09:00
 */

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

/*******  All Required define Pre-Processors and typedef Constants *******/
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define rep(i,n) for(int i=0; i<(n); ++i)
#define rep1(i,n) for(int i=1; i<=(n); ++i)
#define Dcout(a) cout << setprecision(20) << a << endl
#define MP make_pair
#define PB push_back
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
//const ll INF = (1LL << 60);
//const double pi=acos(-1.0);
using ll = long long;
using PAIR = pair<int, int>;
#define debug cout << "line : " << __LINE__ << " debug" << endl
/****** Template of some basic operations *****/
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; }
/**************************************/

/******** User-defined Function *******/
using Graph = vector<vector<int>>;

/**************************************/

/********** Main()  function **********/
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n; cin >> n;
  Graph 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<int> dist(n,-1);
  queue<int> que;
  dist[0] = 0;
  que.push(0);
  while(!que.empty()){
      int v = que.front(); que.pop();
      for(int nv: G[v]){
          if(dist[nv] != -1) continue;
          dist[nv] = dist[v] + 1;
          que.push(nv);
      }
  }

  rep(i,n){
      cout << i+1 << " " << dist[i] << endl;
  }
  return 0;
}
/********  Main() Ends Here *************/

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

vector<vector<ll>> lis;
vector<ll> dset;
ll d=0;

void find(ll p) {
    if(dset[p] == -1 || dset[p] > d) {
        dset[p] = d;
        d++;
        for(ll i:lis[p]) {
            find(i);
        }
        d--;
    }
}

int main() {
    ll n;
    cin >> n;
    for(ll i=0;i<n;i++) {
        ll u,k;
        cin>>u>>k;
        vector<ll> aaa;
        for(ll j=0;j<k;j++) {
            ll tmp;
            cin>>tmp;
            aaa.push_back(tmp-1);
        }
        lis.push_back(aaa);
        dset.push_back(-1);
    }
    find(0);
    for(ll i=0;i<n;i++) {
        cout << i+1<<" "<<dset[i]<<endl;
    }
}

C++: #include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <deque>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>
#include <bitset>
#include <sstream>
#define mop 1000000007

using namespace std;

int m[101][101] = {};
int d[101] = {};
int n;
int INFTY = (1 << 15);
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=1;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=1;i<=n;i++){
        cout << i << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
    }
}
int main() {
    
    cin >> n;
    for (int i=1;i<=n;i++){
        int u,k;
        cin >> u >> k;
        for (int j = 0;j<k;j++){
            int p;
            cin >> p;
            m[i][p] = 1;
           
        }
    }
    bfs(1);

}

C++: #include<iostream>
#include<vector>
#include<deque>
#include<tuple>
using namespace std;

int main(){
  vector<int> ans;
  vector<vector<int> > path_field;
  int n;

  cin >> n;
  n++;
  path_field.resize(n);
  ans.resize(n);
  for(int i=1;i<n;i++){
    ans[i]=-1;
  }
  for(int i=1;i<n;i++){
    int _m,m;

    path_field[i].resize(n);
    for(int j=1;j<n;j++){
      path_field[i][j]=0;
    }

    cin >> _m >> m;
    for(int j=0;j<m;j++){
      int to;
      
      cin >> to;
      path_field[i][to]=1;
    }
  }

  deque<tuple<int,int> > que;

  que.push_back(make_tuple(1,0));
  ans[1]=0;
  while(!que.empty()){
    int& standingby=get<0>(que.front());

    for(int i=1;i<n;i++){
      if((path_field[standingby][i]==1)&&(ans[i]==-1)){
        que.push_back(make_tuple(i,get<1>(que.front())+1));
	ans[i]=get<1>(que.front())+1;
      }
    }
    que.pop_front();
  }

  for(int i=1;i<n;i++){
    cout << i << " " << ans[i] << endl;
  }
}

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

int main(){
  int n;
  cin>>n;
  vector<vector<int>> g(n);
  for(int i=0;i<n;i++){
   int I,k;
    cin>>I>>k;
    for(int j=0;j<k;j++){
      int x;
      cin>>x;
      x--;
      g[i].push_back(x);
    }
  }
  
  	vector<int> d(n,-1);
 	 queue<int> q;
  
 	 d[0]=0;
 	 q.push(0);
  
 	 while(!q.empty()){
  	  int v=q.front(); q.pop();
     
  	  for(int &nv: g[v]){
    	  if(d[nv]!=-1) continue;
       
    	  d[nv]=d[v]+1;
    	  q.push(nv);
      }
     }
  for(int i=0;i<n;i++) cout<<i+1<<" "<<d[i]<<endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
int n, u, v, k, l;
int edge[10001];
int start[101];
int goal[101];
int c;
int bfs[101];
queue<int> Q;
int main()
{
	cin >> n;
	c=0;
	for(int i=1; i<=n; i++)
	{
		cin >> u;
		start[i]=c;
		cin >> k;
		c+=k;
		goal[i]=c;
		for(int j=start[i]; j<goal[i]; j++)
		{
			cin >> edge[j];
		}
	}
	for(int i=1; i<=n; i++)
	{
		bfs[i]=-1;
	}
	bfs[1]=0;
	Q.push(1);
	while(!Q.empty())
	{
		v=Q.front();
		Q.pop();
		for(int j=start[v]; j<goal[v]; j++)
		{
			l=edge[j];
			if(bfs[l]==-1)
			{
				bfs[l]=bfs[v]+1;
				Q.push(l);
			}
		}
	}
	for(int i=1; i<=n; i++)
	{
		cout << i << ' ' << bfs[i] << endl;
	}
	return 0;
}