object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val x :: xs = List.fill(sc.nextInt())(sc.nextInt())
  val (_, sum) = xs.foldLeft((x, 0)) {
    case ((l, acc), r) => (l.max(r), acc + (l - r).max(0))
  }
  println(sum)
}
