### constant

SUM_W = 10

### subroutines

def pack_manju(k, w, mj)
  if k >= 9
    return (w == 0 ? [mj.clone] : [])
  end

  mjs = []
  mjk = $manju[k]

  for i in (0..mjk)
    w0 = w - (k + 1) * i
    if w0 >= 0
      mj[k] = i
      mjs0 = pack_manju(k + 1, w0, mj)
      mjs += mjs0
    end
  end

  mjs
end

def max_count_pack(k, mj, mjs)
  return 1 if k >= mjs.length

  mc0 = max_count_pack(k + 1, mj, mjs)

  mj0 = []
  for i in (0..8)
    mj0[i] = mj[i] + mjs[k][i]
    if mj0[i] > $manju[i]
      return mc0
    end
  end

  mc1 = 1 + max_count_pack(k + 1, mj0, mjs)

  (mc0 > mc1) ? mc0 : mc1
end

### main

while true
  $n = gets.strip.to_i
  break if $n == 0

  $manju = 9.times.map{0}

  gets.strip.split(' ').map{|s| $manju[s.to_i - 1] += 1}
  #p $manju
  #p $manju.inject(1){|s, m| s * m}

  mc = 0

  for i in (0..3)
    m = [$manju[i], $manju[8 - i]].min
    $manju[i] -= m
    $manju[8 - i] -= m
    mc += m
  end

  m = $manju[4] / 2
  $manju[4] -= 2 * m
  mc += m

  #p $manju
  #p $manju.select{|m| m > 0}.inject(1){|s, m| s * m}
  #p mc

  mjs = pack_manju(0, SUM_W, [])
  #p mjs
  #p mjs.length

  mc += max_count_pack(0, [0] * 9, mjs) - 1
  puts mc
end