#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-5;
int main(void) {
  int n;
  cin >> n;
  vector<int> seq(n);
  vector<int> perm(n);
  for (int i = (0); i < (int)(n); ++i) cin >> seq[i];
  for (int i = (0); i < (int)(n); ++i) perm[i] = i + 1;
  if (seq == perm)
    cout << "0 0" << endl;
  else {
    bool ans = true;
    int begin = 0;
    int end = n;
    for (int i = (0); i < (int)(n); ++i) {
      if (seq[i] != perm[i]) {
        begin = i + 1;
        end = seq[i];
        break;
      }
    }
    for (int i = (begin); i < (int)(end); ++i) {
      if (seq[i] - seq[i - 1] != -1) {
        ans = false;
      }
    }
    for (int i = (end); i < (int)(n); ++i) {
      if (seq[i] != perm[i]) {
        ans = false;
      }
    }
    if (ans) {
      cout << begin << " " << end << endl;
    } else {
      cout << "0 0" << endl;
    }
  }
  return 0;
}
