#include <bits/stdc++.h>
using namespace std;
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}
int main() {
  int N;
  cin >> N;
  long long ans = 0;
  vector<int> A(N);
  vector<pair<int, int>> points;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  auto search_end = A.end();
  while (search_end != A.begin()) {
    auto result = max_element(A.begin(), search_end);
    auto distance = result - A.begin();
    points.push_back({*result, distance});
    search_end = result;
  }
  size_t j = 0;
  for (size_t i = A.size(); i > 0; --i) {
    ans += points[j].first - A[i];
    if (i == points[j].second) {
      if (j != points.size()) j++;
    }
  }
  cout << ans - N << endl;
}
