#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int data[n + 2];
  data[0] = 0;
  data[n + 1] = n + 1;
  for (int i = 1; i <= n; i++) cin >> data[i];
  int l = 1;
  while (l <= n && data[l] - data[l - 1] == 1) l++;
  int r = n;
  while (r >= 0 && data[r + 1] - data[r] == 1) r--;
  bool ok = l < r;
  for (int i = l + 1; ok && i <= r; i++) {
    ok &= data[i - 1] - data[i] == 1;
  }
  if (ok)
    cout << l << ' ' << r << '\n';
  else
    cout << "0 0\n";
  return 0;
}
