#!/usr/bin/env ruby
def read_input()
  edge_num = gets(chomp: true).to_i
  edges = []
  graph = {}
  (1...edge_num).reduce([edges,graph]) do |acc,_|
    edges, graph = acc
    edge = gets(chomp: true).split()
    edges << edge
    a,b = edge
    if (!graph.has_key?(a))
      graph[a] = []
    end
    graph[a] << b
    [edges, graph]
  end
end

def select_color(current, parent_color)
  if (current + 1 == base_color)
    current + 2
  else
    current + 1
  end
end

def calc_max(max, children_num, is_root)
  edge_num = if (is_root)
               children_num + 1
             else
               children_num
             end
  if (children_num > max)
    max
  else
    edge_num
  end
end

def solve(graph, node, parent_color, color_edge, max)
  children = graph[node]
  return [max, color_edge] if children.nil?
  max = calc_max(max, children.length, parent_color.nil?)
  color = 0
  children.reduce([max, color_edge]) do |acc, child|
    max, color_edge = acc
    color = select_collor(color, parent_color)
    color_edge[node] = {} if (color_edge[node].nil?)
    color_edge[node][child] = color
    solve(graph, child, color, color_edge, max)
  end
end

edges, graph = read_input
max, color_edge = solve(graph, "1", nil, {}, -1)

puts max
edges.each do |a,b|
  puts color_edge[a][b]
end
