Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	dim, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

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

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			if !dl.isVisited(v) {
				stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
			}
		}
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

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