let rec solve trace (acc, cas) (c, a) =
  match cas with
  | [] -> (acc, List.rev_append trace [(c, a)])
  | (c', a') :: cas' ->
      if c = c' && a <= a' then
        (acc + List.length cas, List.rev_append trace ((c, a) :: cas))
      else
        solve ((c', a') :: trace) (acc, cas') (c, a)

let () = Scanf.scanf "%d\n" @@ fun n ->
  let cas = Array.init (2 * n) @@ fun _ -> Scanf.scanf "%c %d\n" @@ fun c a -> c, a in
  Printf.printf "%d\n" @@ fst @@ Array.fold_left (solve []) (0, []) cas

