Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

func pop(q *[]int) int {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

C++: #include<iostream>
#include<queue>
using namespace std;
int N;
int D[100];
void bfs(int src){
    int u[100],k[100];
    int d[100][100],a[100];
    bool visit[100];
    for(int i=1;i<=N;++i){
        cin>>u[i]>>k[i];
        for(int j=0;j<k[i];++j){
            cin>>a[j];
            d[u[i]][a[j]]=1;
        }
        visit[i]=true;
    }
    queue<int>Q;
    Q.push(src);
    D[src]=0;
    visit[src]=false;
    while(!Q.empty()){
        int cur=Q.front();
        Q.pop();
        for(int i=1;i<=N;++i){
            if(d[cur][i]==1&&visit[i]){
                visit[cur]=false;
                visit[i]=false;
                D[i]=D[cur]+1;
                Q.push(i);
            }
        }
    }
    for(int i=1;i<=N;++i){
        cout<<i<<" "<<D[i]<<endl;
    }
}
int main(){
    cin>>N;
    int start=1;
    bfs(start);
}

C++: 
#include <bits/stdc++.h>

#define PI 3.141592653
#define rep(i,a,n) for(int i=a;i<(int)n;++i)
#define SZ(x) ((int)(x).size())	//size() unsigned -> int
#define descSort(a) sort(a.begin(),a.end(),std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;
ll gcd(ll x,ll y){
    if(x%y==0)return y;
    return gcd(y,x%y);
}

int main(void){
    int n;
    cin>>n;
    vector<vector<bool>> graph(n,vector<bool>(n));
    rep(i,0,n){
        int u,k;
        cin>>u>>k;
        u--;
        rep(j,0,k){
            int v;
            cin>>v;
            v--;
            graph[u][v]=true;
        }
    }
    vector<int> d(n,-1);
    queue<int> q;
    q.push(0);
    d[0]=0;
    while(!q.empty()){
        int now=q.front();q.pop();
        for(int i=0;i<n;i++){
            if(graph[now][i] && d[i]==-1){
                q.push(i);
                d[i]=d[now]+1;
            }
        }
    }
    rep(i,0,n){
        cout<<i+1<< " "<< d[i]<<endl;
    }
    return 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;
  }
}