Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

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

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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

using namespace std;

#define MAX 100
int INFTY = (1<<21);
int num,M[MAX][MAX];
int d[MAX];

void bfs(int s){
  queue<int> q;
  q.push(s);
  int u,i,v;
  for( i = 0; i < num; i++){
    //d[i] = INFINITY;
    d[i] = INFTY;
  }
  d[s] = 0;
  while( !q.empty()){
    u = q.front();
    q.pop();
  for(v = 0; v < num; v++){
    if( M[u][v] == 0){
      continue;
    }
    //if( d[v] != INFINITY){
    if( d[v] != INFTY){
      continue;
    }
    d[v] = d[u]+1;
    q.push(v);
  }
  }
  for(i = 0; i < num; i++){
    //cout << i+1 << " ";
    /*
    if(d[i] == INFINITY){
      cout << (-1);
    }
    else{
      cout << d[i] << endl;
    } 
    */
    cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
  }
}

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

  for(i = 0; i < num; i++){
    cin >> u >> k;;
    u--;
    for(j = 0; j < k; j++){
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);
  return 0;
}

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

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

struct Vertex {
    vector<ll> node;
};

Vertex V[105];
ll C[105];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    REP(i, N) {
        C[i] = INF;
    }
    REP(i, N) {
        ll n, k;
        cin >> n >> k;
        n--;
        REP(_, k) {
            ll v;
            cin >> v;
            v--;
            V[n].node.push_back(v);
        }
    }

    queue<pll> q;
    q.push(pll(0, 0));
    C[0] = 0;
    while (!q.empty()) {
        pll t = q.front();
        q.pop();
        ll cost = t.second + 1;
        for (auto &n:V[t.first].node) {
            if (C[n] <= cost)
                continue;
            C[n] = cost;
            q.push(pll(n, cost));
        }
    }

    REP(i, N) {
        cout << i + 1 << " ";
        if (C[i] == INF)
            cout << -1;
        else
            cout << C[i];
        cout << endl;
    }
    return 0;
}

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

using ll = long long;
using namespace std;
int n, A[100][100];
int d[100];
/* initialize */

void bfs(int s){
    queue<int> q;
    q.push(s);
    for (int i = 0; i < n; ++i) d[i] = 1<<30;
    d[s] = 0;
    int u;
    while(!q.empty()){
        u = q.front(); q.pop();
        for (int i = 0; i < n; ++i) {
            if ( A[u][i] == 0 ) continue;
            if (d[i] != 1<<30) continue;
            d[i] = d[u] + 1;
            q.push(i);
        }
    }
    for (int i = 0; i < n; ++i) {
        cout << i+1 << " " << ( (d[i] == 1<<30) ? -1 : d[i]) << "\n";
    }
}
/* main */
int main() {
    int u, k, v;
    cin >> n;
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) A[i][j] = 0;
    for (int i = 0; i < n; ++i) {
        cin >> u >> k;
        for (int j = 0; j < k; ++j) {
            cin >> v;
            A[u-1][v-1] = 1;
        }
    }
    bfs(0);
    return 0;
}

C++: // C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int ll
#define loop(__x, __start, __end) for(Int __x = __start; __x < __end; __x++)
#define loopdown(__x, __start, __end) for(Int __x = __start; __x > __end; __x--)
#define rep(__n) for(Int __x = 0; __x < __n; __x++)
#define span(__a,__x,__y) __a.begin()+__x,__a.begin()+__y
#define span_all(__a) __a.begin(),__a.end()
#define len(__x) (__x.size())
#define last(__x) (__x[__x.size()-1])

using namespace std;

struct Point {
  Int x, y;
  Point(Int x, Int y): x(x), y(y) {}
  Point(): x(0), y(0) {}

  bool operator< (const Point &p1) const {
    return x < p1.x && y < p1.y;
  }
};
ostream & operator << (ostream & out, Point const & v) {
  out << "Point(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Point & v) {
  Int x, y;
  in >> x; in >> y; v.x = x; v.y = y;
  return in;
}

// Geometry

#define EPS 0.0000000001

class Vector2 {
public:
  double x, y;
  
  Vector2(double x = 0, double y = 0): x(x), y(y) {}
  
  Vector2 operator + (Vector2 v) { return Vector2(x + v.x, y + v.y); }
  Vector2 operator - (Vector2 v) { return Vector2(x - v.x, y - v.y); }
  Vector2 operator * (double k) { return Vector2(x * k, y * k); }
  Vector2 operator / (double k) { return Vector2(x / k, y / k); }
  
  double length() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  double dot (Vector2 v) { return x * v.x + y * v.y; }
  double cross (Vector2 v) { return x * v.y - y * v.x; }
  
  bool operator < (const Vector2 &v) const {
    return x != v.x ? x < v.x : y < v.y;
  }
  
  bool operator == (const Vector2 &v) const {
    return fabs(x - v.x) < EPS && fabs(y - v.y) < EPS;
  }
};

ostream & operator << (ostream & out, Vector2 const & v) { 
  out<< "Vector2(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Vector2 & v) { 
  double x, y;
  in >> x;
  in >> y;
  v.x = x;
  v.y = y;
  return in;
}

Int N;
vector<vector<Int> > G(101);
vector<Int> D(101, -1);

void input() {
  cin >> N;
  Int u, c, v;
  rep(N) {
    cin >> u >> c;u--;
    rep(c) {
      cin >> v;v--;
      G.at(u).push_back(v);
    }
  }
}

void bfs() {
  queue<Int> Q;
  Q.push(0);
  D.at(0) = 0;
  while (Q.size()) {
    Int u = Q.front();Q.pop();
    for (auto v: G.at(u)) {
      if (D.at(v) != -1) continue;
      Q.push(v);
      D.at(v) = D.at(u) + 1;
    }
  }
}

void solve() {
  bfs();
  for (int i=0; i<N; i++) {
    cout << i+1 << " " << D.at(i) << endl;
  }
}

int main() {
  input();
  solve();
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0;}

int n;
vector<vector<int> >g;
int dist[110];
void bfs(int u) {
    queue<int> q;
    q.push(u);
    rep(i,n) dist[i] = -1;
    dist[u] = 0;
    while(!q.empty()) {
        int v = q.front();q.pop();
        for(int i = 0;i<g[v].size();i++) {
            int nv = g[v][i];
            if(dist[nv] == -1) {
                q.push(nv);
                dist[nv] = dist[v] + 1;
            }               
        }
    }
}
int main() {
    cin >> n;
    g.assign(n,vector<int>());
    rep(i,n) {
        int u,k;
        cin >> u >> k;
        u--;
        rep(j,k) {
            int v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    bfs(0);
    rep(i,n) cout << i+1 << " " << dist[i] << endl;
}