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 <bits/stdc++.h>
using namespace std;

#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
//constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

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;
}

struct Edge {
    ll to, rev;
    long double cap;
    Edge(ll _to, long double _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag,
              long double revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve() {
    ll n;
    cin >> n;
    vvec<int> g(n);
    REP(i, n) {
        int u, k;
        cin >> u >> k;
        u--;
        REP(i, k) {
            int v;
            cin >> v;
            v--;
            g[u].push_back(v);
        }
    }
    vec<ll> d(n, INF);
    queue<ll> que;
    que.push(0);
    d[0] = 0;
    while(que.size()) {
        int v = que.front();
        que.pop();
        for(auto to : g[v]) {
            if(d[to] != INF)
                continue;
            d[to] = d[v] + 1;
            que.push(to);
        }
    }

    REP(i, n) {
        if(d[i] == INF)
            cout << i + 1 << " " << -1 << en;
        else
            cout << i + 1 << " " << d[i] << en;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    solve();

    /*
    ll t;
    cin >> t;
    REP(i, t) { solve();}
    */

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, to) for (int i = 0; i < (to); i++)
#define rrep(i, to) for (int i = (to) - 1; i >= 0; i--)
#define repf(i, from, to) for (int i = (from); i < (to); i++)
#define all(v) v.begin(), v.end()
#define unless(cond) if (!(cond))
using namespace std;
using ll = long long;
template <typename T>
using V = vector<T>;
template <typename T, typename U>
using P = pair<T, U>;

V<int> dist;
V<V<int>> graph;

int main() {
  int n;
  cin >> n;

  graph.resize(n);
  dist.resize(n, -1);

  rep(i, n) {
    int u, k;
    cin >> u >> k;
    u--;

    rep(j, k) {
      int adj;
      cin >> adj;
      adj--;
      graph[u].push_back(adj);
    }

    dist[i] = -1;
  }


  queue<int> will;
  dist[0] = 0;
  will.push(0);

  while(!will.empty()) {
    int current_i = will.front();
    will.pop();

    for(auto next_i : graph[current_i]) {
      if(dist[next_i] != -1) {
        continue;
      }
      dist[next_i] = dist[current_i] + 1;
      will.push(next_i);
    }
  }


  rep(i, n) {
    cout << i + 1 << " " << dist[i] << endl;
  }
}

C++: #include <iostream>
#include <queue>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int Adj[n][n] = {};
    int d[n] = {};
    for (int i = 0; i < n; i++)
    {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            Adj[u - 1][v - 1] = 1;
        }
        d[i] = -1;
    }

    int src = 0;
    queue<int> q;
    d[src] = 0;
    q.push(src);
    while (!q.empty())
    {
        int cur = q.front();
        q.pop();
        for (int dst = 0; dst < n; dst++)
        {
            if (Adj[cur][dst] && d[dst] == -1)
            {
                d[dst] = d[cur] + 1;
                q.push(dst);
            }
        }
    }

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

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>
#include <iomanip>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL<<60;
const double PI=3.14159265358979323846;
const int NMAX=100005;
using namespace std;

vector<int> to[101];
vector<int> d(101,-1);

int main(){
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        int u, k;
        cin >> u >> k;
        for(int j = 0; j < k; j++){
            int v;
            cin >> v;
            to[u].push_back(v);
        }
    }

    queue<pair<int, int> > q;
    q.push(make_pair(1,0));

    while(q.size() > 0){
        pair<int, int> p = q.front();
        q.pop();
        int i = p.first;
        int t = p.second;

        if(d[i] != -1) continue;
        d[i] = t;
        for(int j = 0; j < to[i].size(); j++){
            q.push(make_pair(to[i][j], t+1));
        }
    }

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

    return 0;
}

C++: #include <iostream>
#include<bits/stdc++.h>
using namespace std;
int n,m,k,a,b,ch[100];
vector<int>v[100];
queue<int>que;
int main(void){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>m>>k;
        for(int i=0;i<k;i++){
            cin>>a;
            v[m-1].push_back(a-1);
        }
    }
    que.push(0);
    ch[0]=1;
    while(que.size()){
        int q=que.front();
        que.pop();
        for(auto x:v[q]){
            if(ch[x]==0){
                ch[x]=ch[q]+1;
                que.push(x);
            }
        }
    }
    for(int i=0;i<n;i++){
        cout<<i+1<<" "<<ch[i]-1<<endl;
    }
}