Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
struct edge {
    int to;     // 辺の行き先
    int weight; // 辺の重み
    edge(int t, int w) : to(t), weight(w) { }
};
using Graph = vector<vector<int>>;
using dou =long double;
string yes="yes";
string Yes="Yes";
string YES="YES";
string no="no";
string No="No";
string NO="NO";

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define rep(i, n)         for(int i = 0; i < (int)(n); i++)
#define brep(n)           for(int bit=0;bit<(1<<n);bit++)
#define erep(i,container) for (auto i : container)
#define irep(i, n)        for(int i = n-1; i >= (int)0; i--)
#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)
#define reprep(i,j,h,w) rep(i,h)rep(j,w)
#define all(x) (x).begin(),(x).end()
#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
#define sum(a) accumulate(all(a),0ll)
#define keta fixed<<setprecision
#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))
#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));
#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));
#define case std::cout << "Case #" <<qqq+1<<": "
#define res resize
#define as assign
#define ffor for(;;)
#define ppri(a,b) std::cout << a<<" "<<b << std::endl
#define pppri(a,b,c) std::cout << a<<" "<<b <<" "<< c<<std::endl
#define aall(x,n) (x).begin(),(x).begin()+(n)
 
typedef long long ll;
typedef pair<int,int> P;
//typedef long long T;
const int INF = 2000000000;
const ll INF64 = 922337203685477580ll;
const ll MOD = 1000000007ll;
int main(){
    int n;
    std::cin >> n;
    std::vector<std::vector<int> >g(n);
    rep(i,n){
        int d,k;
        std::cin >> d>>k;
        d--;
        rep(j,k){
            int dd;
            std::cin >>dd;
            g[d].pb(dd-1);
        }
    }
    std::vector<int> di(n,-1);
    std::queue<int> q;
    di[0]=0;
    erep(i,g[0]){
        di[i]=1;
        q.push(i);
    }
    while(!q.empty()){
        int p=q.front();
        erep(i,g[p]){
            if(di[i]==-1){
                di[i]=di[p]+1;
                q.push(i);
            }
        }
        q.pop();
    }
    rep(i,n){
        ppri(i+1,di[i]);
    }
}

C++: #include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

using ll = long long;

int main() {
    int n, u, k, v;
    cin >> n;
    vector<vector<int>> edge(n);
    vector<int> d(n, -1);
    rep(i, n) {
        cin >> u >> k;
        rep(j, k) {
            cin >> v;
            edge[u - 1].push_back(v - 1);
        }
    }
    queue<pair<int, int>> q;
    q.push(make_pair(0, 0));
    d[0] = 0;
    while (!q.empty()) {
        auto p = q.front();
        q.pop();
        int v0 = p.first, d0 = p.second;
        rep(i, edge[v0].size()) {
            int v1 = edge[v0][i];
            if (d[v1] != -1) continue;
            q.push(make_pair(v1, d0 + 1));
            d[v1] = d0 + 1;
            //cout << "d[" << v1 << "]=" << d0 + 1 << "\n";
        }
    }
    rep(i, n) {
        cout << i + 1 << " " << d[i] << "\n";
    }
}

C++: #include <bits/stdc++.h>
#define int long long
#define rng(i, l, r) for (size_t i = (l); i < (r); ++i)
#define rep(i, n) rng(i, 0, n)
#define gnr(i, l, r) for (size_t i = (r)-1; i >= (l); i--)
#define per(i, b) gnr(i, 0, b)
#define ALL(obj) (obj).begin(), (obj).end()    //1,2,3,...
#define rALL(obj) (obj).rbegin(), (obj).rend() //...,3,2,1

using namespace std;
const int INF = 1e18;

struct edge
{
    int to;
    int cost;
};
using Graph = vector<vector<edge>>;

#define WHITE 0
#define GRAY 1
#define BLACK 2

int n;
Graph G;

void solve()
{

    // remove the bottom 3 lines when you submit this code.
    std::ifstream in("sample.txt");
    std::cin.rdbuf(in.rdbuf());

    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    Graph G(n);
    for (int i = 0; i < n; i++)
    {
        int u, k;

        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            v--;
            G[u].push_back((edge){v, 1});
        }
    }

    queue<int> que;
    int dist[110];
    int seen[110];
    for (int i = 0; i < 110; i++)
    {
        dist[i] = INF;
        seen[i] = WHITE;
    }

    que.push(0);
    seen[0] = GRAY;
    dist[0] = 0;
    while (!que.empty())
    {
        int now = que.front();
        que.pop();
        seen[now] = BLACK;
        for (auto ne : G[now])
        {
            int v = ne.to;
            int vcost = ne.cost;
            if (seen[v] == BLACK)
                continue;
            dist[v] = dist[now] + 1;
            que.push(v);
            seen[v] = BLACK;
        }
    }

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

signed main()
{
    solve();
    return 0;
}

C++: #include <iostream>
#include <queue>


using namespace std;


static const  int N =  100;
static const  int no =  -2;

int graph[N][N] = {{}};
int d[N] = {};
int n;

void bfs(int v){
  queue<int> Q; 
  Q.push(v);

  int u;
    for(int i = 1;i <= n; i++){
      d[i] = no;
    
}
  d[v] = 0;

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

    for(int i = 1;i <= n; i++){
      if(graph[u][i] == 0) continue;
      if(d[i] != no) continue;
      d[i] = d[u] +1;
      Q.push(i);
    }
  }
}


int main(){
  int u,v,k;
  //graph
  cin >> n ;

  for(int i = 0; i < n; i++){
    cin >> u >> k ;
    for(int j = 0; j < k ; j++){
      cin >> v;
      graph[u][v] = 1;
    }
  }


  bfs(1);

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

  return 0;
}