class Line
  attr_reader :first, :last
  def initialize first, last
    @first, @last = first, last
  end

  # return lines
  def - other
    if first >= other.first && last <= other.last
      # hidden
      []
    elsif first < other.first && last > other.last
      # head & tail
      [Line.new(first, other.first), Line.new(other.last, last)]
    elsif first < other.first && last > other.first
      # head
      [Line.new(first, other.first)]
    elsif first < other.last && last > other.last
      # tail
      [Line.new(other.last, last)]
    else
      # all
      [self]
    end
  end

  # return lines
  def & other
    p_first = [first, other.first].max
    p_last = [last, other.last].min
    p_first < p_last ? [Line.new(p_first, p_last)] : []
  end

  def length
    last - first
  end

  def cover? i
    i >= first && i <= last
  end
end

class Rect
  attr_reader :x1, :y1, :x2, :y2
  def initialize x1, y1, x2, y2
    @x1, @y1, @x2, @y2 = x1, y1, x2, y2
  end

  def area
    x_line.length * y_line.length
  end

  # return rects
  def - other
    left_right = (x_line - other.x_line).map {|x_s_line|
      Rect.new(x_s_line.first, y1, x_s_line.last, y2)
    }.to_a

    up_down = (x_line & other.x_line).flat_map {|x_p_line|
      (y_line - other.y_line).map {|y_s_line|
        Rect.new(x_p_line.first, y_s_line.first, x_p_line.last, y_s_line.last)
      }
    }.to_a

    left_right.concat(up_down)
  end


  def x_line
    Line.new x1, x2
  end

  def y_line
    Line.new y1, y2
  end
end

class RectWithValidPerimeter
  attr_reader :rect
  attr_accessor :left_lines, :right_lines, :up_lines, :down_lines

  def initialize rect
    @rect = rect
    @left_lines = [Line.new(rect.y1, rect.y2)]
    @right_lines = [Line.new(rect.y1, rect.y2)]
    @up_lines = [Line.new(rect.x1, rect.x2)]
    @down_lines = [Line.new(rect.x1, rect.x2)]
  end

  def prune_valid_perimeter! other_rect, remain_when_same_line
    if other_rect.x_line.cover?(rect.x1) && (!remain_when_same_line || !(other_rect.x1 == rect.x1))
      #left
      @left_lines = prune_valid_lines @left_lines, other_rect.y_line
    end
    if other_rect.x_line.cover?(rect.x2) && (!remain_when_same_line || !(other_rect.x2 == rect.x2))
      #right
      @right_lines = prune_valid_lines @right_lines, other_rect.y_line
    end
    if other_rect.y_line.cover?(rect.y1) && (!remain_when_same_line || !(other_rect.y1 == rect.y1))
      #up
      @up_lines = prune_valid_lines @up_lines, other_rect.x_line
    end
    if other_rect.y_line.cover?(rect.y2) && (!remain_when_same_line || !(other_rect.y2 == rect.y2))
      #down
      @down_lines = prune_valid_lines @down_lines, other_rect.x_line
    end
  end

  def valid_perimeter
    [@left_lines, @right_lines, @up_lines, @down_lines]
      .flat_map {|lines| lines.map(&:length) }
      .reduce(0, :+)
  end

  private
  def prune_valid_lines valid_lines, deleter_line
    valid_lines.flat_map {|valid_line| valid_line - deleter_line }
  end
end

def calc_area rects
  extract_unique_rects([], rects.dup)
    .lazy
    .map(&:area)
    .reduce(0, :+)
end

def extract_unique_rects fixed_rects, remain_rects
  return fixed_rects if remain_rects.empty?
  return extract_unique_rects(
    fixed_rects.concat(fixed_rects.reduce([remain_rects.pop]) {|parts, fixed_rect|
      parts.flat_map {|part| part - fixed_rect }
    }),
    remain_rects
  )
end

def calc_perimeter rects
  units = rects.map {|r| RectWithValidPerimeter.new(r) }
  units.combination(2).each {|(a, b)|
    a.prune_valid_perimeter! b.rect, true
    b.prune_valid_perimeter! a.rect, false
  }
  units
    .lazy
    .map {|u| u.valid_perimeter }
    .reduce(0, :+)
end

Enumerator.new {|y|
  until ((n_r = gets.split(" ").map(&:to_i)) == [0, 0])
    y << [[method(:calc_area), method(:calc_perimeter)].take(n_r[1]), n_r[0].times.map { Rect.new(*gets.split(" ").map(&:to_i)) }]
  end
}
  .flat_map{|(resolvers, rects)| resolvers.map {|r| r[rects] } }
  .each {|r| puts r }