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

/*

void bfs(int vertex, int depth)
{
  seen[vertex] = true;
  ans [vertex] = depth;
  for (auto &&i : G[vertex]) que.push(i);
  while(!(que.empty()))
  {
    int next = que.front();
    que.pop();
    if (seen[next] == false)
    {
      bfs(next,depth + 1);
    }
  }

  
}




int main()
{
  fill_n(ans,105,-1);
  fill_n(seen,105,false);
  int n;cin>>n;
  for (int i = 0; i < n; i++)
  {
    int u;cin>>u;
    int k;cin>>k;
    for (int j = 0; j < k; j++)
    {
      int v;cin>>v;
      G[u].push_back(v);
    }
    
  }

}
*/



  
int ans[105];
vector<int> G[105];
queue<int> que;

int main()
{
  fill_n(ans,105,-1);
  int n;cin>>n;
  for (int i = 0; i < n; i++)
  {
    int u;cin>>u;
    int k;cin>>k;
    for (int j = 0; j < k; j++)
    {
      int v;cin>>v;
      G[u].push_back(v);
    }
    
  }


/*
  for (int i = 1; i <= n; i++)
  {
    cout << i << ": " ;
    for (auto &&j : G[i])
    {
      cout << j << " ";
    }
    cout << endl;
    
  }
*/


  que.push(1);
  ans[1] = 0;

  while (!que.empty())
  {
    int vertex = que.front();
    que.pop();

    for (auto &&next : G[vertex])
    {
      if (ans[next] != -1) continue;
      que.push(next);
      ans[next] = ans[vertex] + 1;
    }
  

  }
  

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

}

C++: // -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_11_C: Breadth First Search

#include <algorithm>
#include <cstdio>
#include <queue>

using uint = unsigned int;

class Graph {
  const uint n_;
  uint* const g_;
  int* const depth_;
  
public:
  Graph(uint n) : n_(n),
                  g_(new uint[(n_*n_)]),
                  depth_(new int[n])
  {
    std::fill(g_, g_+(n_*n_), 0u);
  }

  ~Graph()
  {
    delete[] g_;
    delete[] depth_;
  }
  
  uint* operator [](uint i) const { return g_+(n_*i); }

  int* depth() const { return depth_; }

  void bfs()
  {
    init_depth();
    std::queue<uint> q;
    auto i = 0u;
    q.push(i);
    depth_[i] = 0;
    while (!q.empty()) {
      i = q.front();
      for (auto j = 0u; j < n_; ++j) {
        if ((*this)[i][j] && depth_[j] == -1) {
          q.push(j);
          depth_[j] = depth_[i]+1;
        }
      }
      q.pop();
    }
  }

private:
  void init_depth()
  {
    std::fill(depth_, depth_+(n_), -1);
  }
};

int main()
{
  uint n;
  std::scanf("%u", &n);

  Graph g(n);

  uint u, k, v;
  for (auto i = 0u; i < n; ++i) {
    std::scanf("%u %u", &u, &k);
    for (auto j = 0u; j < k; ++j) {
      std::scanf("%u", &v);
      g[u-1][v-1] = 1;
    }
  }

  g.bfs();
  auto depth = g.depth();
  for (auto i = 0u; i < n; ++i) {
    std::printf("%u %d\n", i+1, depth[i]);
  }
}

// eof

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

using namespace std;

static const int N = 100;

int n,d[N],M[N][N];//d[]...?§??????????????????¢(-1?????°?????????)???M[][]?????£??\???????????§????????????

void dfs(int r) {
	queue<int> q;//queue??§?????????
	q.push(r);//?§?????????£??\
	for (int i = 0; i < n; i++) d[i] = -1;//??????????????????
	d[r] = 0;//?§????????????¢???0

	int u;
	while (!q.empty()) {//??¢?´¢???????????§??°?????????
		u = q.front();//u????????\?????????????????¢?´¢???????????£?????????????????£??\
		q.pop();//?????\???????????????????´????????????????

		for (int v = 0; v < n; v++) {//??£??\????????????????????????
			if (M[u][v] == 0) continue;//?§??????¨??????????????§?????°?????????????????´???????????????
			if (d[v] != -1) continue;//?§??????¨????????¢???????????£??????????????¢?´¢???????????£??????????????´??????????????????
			d[v] = d[u] + 1;//??????u????§??????¨??£??\?????????????????????????????¢1???1?????????????????????????????????+1???
			q.push(v);//?????\??????v?????????
		}
	}
	for (int i = 0; i < n; i++) {
		cout<<i+1<<" "<<d[i]<<endl;//??????
	}
}

int main() {
	int u, p, v;
	cin >> n;
	for (int i = 0; i < n; i++) {//?????????(d[]?????¢????????????dfs??§?±??????????????????§????????????????????????)
		for (int j = 0; j < n; j++) {
			M[i][j] = 0;
		}
	}

	for (int i = 0; i < n; i++) {//??????????????????
		cin >> u >> p;
		u--;//0?????????????????????
		for (int j = 0; j < p; j++) {
			cin >> v;
			v--;//0?????????????????????
			M[u][v] = 1;
		}
	}

	dfs(0);//??????0????§??????¨??????

	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

class DirectedGraph
{
public:
    DirectedGraph() {}
    DirectedGraph(int n)
    : g(vector< vs32 >(n)), dist(vs32(n, -1))
    {}
    ~DirectedGraph() {}

    void bfs(int u)
    {
        int d = 0;
        dist[u]  = 0;
        queue<int> q;
        q.push(u);
        while (!q.empty())
        {
            int n = q.size();
            ++d;
            rep (i, n)
            {
                int v = q.front();
                q.pop();
                rep (j, g[v].size())
                {
                    if (dist[g[v][j]] < 0)
                    {
                        dist[g[v][j]] = d;
                        q.push(g[v][j]);
                    }
                }
            }
        }
    }

    vector< vs32 > g;
    vs32 dist;
};

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

    int n;
    cin >> n;

    DirectedGraph dg(n);
    int u, v;
    int c;
    rep (i, n)
    {
        cin >> u >> c;
        --u;
        dg.g[u].resize(c);
        rep (j, c)
        {
            cin >> v;
            --v;
            dg.g[u][j] = v;
        }
    }

    dg.bfs(0);

    rep (i, n)
    {
        cout << i + 1 << " " << dg.dist[i] << "\n";
    }
    return 0;
}