import 'dart:io';

void main() {
  int n = int.parse(stdin.readLineSync());
  List aList =
      stdin.readLineSync().split(" ").map((x) => int.parse(x)).toList();
  int sum = 0;
  int s = 0;

  for (int i = 1; i < n; i++) {
    int diffLength = aList[s].toString().length - aList[i].toString().length;
    if (diffLength < 0) {
      s = i;
    } else {
      int diff = (aList[s] - (aList[i]));
      if (diff > 0) {
        sum = sum + diff;
      } else if (diff < 0) {
        s = i;
      }
    }
  }
  print(sum);
}
