const input = require('fs').readFileSync('/dev/stdin', 'utf8');

const inputs = input.trim().split('\n');
const n = Number(inputs.shift());
const adj = inputs.map((s) => s.split(' ').map(Number));

class Queue {
  constructor() {
    this.list = [];
  }
  isEmpty() {
    return !this.list.length;
  }
  enqueue(item) {
    this.list.push(item);
  }
  dequeue() {
    return this.list.shift()
  }
}

class Node {
  constructor(id) {
    this.id = id;
    this.list = [];
    this.params = new Map();
  }
  add(node) {
    this.list.push(node);
  }
  get(key) {
    return this.params.get(key);
  }
  set(key, value) {
    return this.params.set(key, value);
  }
  has(key) {
    return this.params.has(key);
  }
  toString() {
    return [this.id, this.has('depth') ? this.get('depth') : -1].join(' ');
  }
}

class Graph {
  constructor() {
    this.map = new Map();
    this.queue = new Queue();
  }
  setNode(node) {
    this.map.set(node.id, node);
  }
  hasNode(id) {
    return this.map.has(id);
  }
  getNode(id) {
    return this.map.get(id);
  }
  bfs1() {
    while (!this.queue.isEmpty()) {
      const parent = this.queue.dequeue();
      parent.list.forEach(child => {
        if (child.has('depth')) {
          return;
        }
        child.set('depth', parent.get('depth') + 1);
        this.queue.enqueue(child);
      });
    }
  }
  bfs2(depth, ...nodes) {
    const children = [];
    nodes.forEach(node => {
      if (node.has('depth')) {
        return;
      }
      node.set('depth', depth);
      children.push(...node.list);
    });
    children.length && this.bfs2(depth + 1, ...children);
  }
  print() {
    for (let i = 1; i <= this.map.size; i++) {
      const node = this.map.get(i);
      console.log(node.toString());
    }
  }
}

const graph = new Graph();

adj.forEach(([id, k, ...ck]) => {
  graph.hasNode(id) || graph.setNode(new Node(id));
  const node = graph.getNode(id);
  ck.forEach(c => {
    graph.hasNode(c) || graph.setNode(new Node(c));
    const child = graph.getNode(c);
    node.add(child);
  });
});

const root = graph.getNode(1);
// root.set('depth', 0);
// graph.queue.enqueue(root);
graph.bfs2(0, root);

graph.print();

