let rec select acc trace = function
  | [] -> acc
  | x :: xs -> select ((trace, x, xs) :: acc) (x :: trace) xs
let select xs = select [] [] xs

let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init (2 * n) @@ fun _ ->
    Scanf.scanf "%c %d\n" @@ fun c a -> c, a in
  Array.fold_left (fun (acc, lst) (c, a) ->
    try
      select lst
      |> List.find (fun (left, p, _) ->
          List.for_all (fun (c', a') -> c <> c' || a' <= a) (p :: left))
      |> (fun (left, p, right) ->
          acc + List.length right,
          List.rev_append (p :: left) ((c, a) :: right))
    with Not_found ->
      acc + List.length lst, (c, a) :: lst ) (0, []) as_
  |> fst
  |> Printf.printf "%d\n"