#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> A(n, 0);
  for (int i = 0; i < n; i++) cin >> A[i];
  int r1 = 0, r2 = n - 1;
  int t = 1;
  while (r1 < n && A[r1] == t) {
    t++;
    r1++;
  }
  t = n;
  while (r2 >= 0 && A[r2] == t) {
    t--;
    r2--;
  }
  bool b = true;
  t = r2 + 1;
  for (int i = r1; i < r2 + 1; i++) {
    if (A[i] != t) b = false;
    t--;
  }
  if (b && r1 < r2)
    cout << r1 + 1 << ' ' << r2 + 1 << endl;
  else
    cout << "0 0" << endl;
  cin >> n;
}
