#!/usr/bin/ruby

sums = Hash.new {|h,k| h[k] = []}
(0..9).each do |a|
  (0..9).each do |b|
    sums["#{a+b}"].push("#{a}#{b}")
  end
end

m = ARGF.gets.to_i
players = Array.new(m).map do
  d, c = ARGF.gets.split
  d * c.to_i
end.join('')

pairs = (0..18).to_a.reverse.map(&:to_s)
count = 0
replaced = true

while players.length > 1
  replaced = false
  begin
    pairs.each do |p1|
      sums[p1].each do |p2|
        sum = 0
        #      $stderr.puts "#{p2} => #{p1}: from #{players}"
        players.sub!(p2) do
          sum += 1
          p1
        end
        next if sum == 0
        replaced = true
        count += sum
        #      $stderr.puts "#{p2} => #{p1}: from #{players}: #{sum}"
        raise "retry"
      end
    end
  rescue
  else
    break
  end
end

#$stderr.puts players
puts count
