#include <bits/stdc++.h>
int main() {
  int n = 5;
  long long int a[] = {2, 1, 5, 4, 3};
  long long int res = 0, mx = -1;
  for (int i = 0; i < (int)(n); i++) {
    res += mx - a[i] > 0 ? mx - a[i] : 0;
    mx = mx > a[i] ? mx : a[i];
  }
  printf("%lld\n", res);
  return 0;
}
