#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    v[i]--;
  }
  int index = -1;
  for (int i = 0; i < n and index == -1; i++) {
    if (v[i] != i) {
      index = i;
    }
  }
  if (index == -1) {
    cout << "0 0" << endl;
  } else {
    int i1 = index;
    int i2 = v[index];
    bool error = false;
    while (i1 <= i2 and not error) {
      error = not(v[i1] == i2 and v[i2] == i1);
      i1++;
      i2--;
    }
    for (int i = v[index] + 1; i < n and not error; i++) error = (v[i] != i);
    if (error)
      cout << "0 0" << endl;
    else
      cout << index + 1 << " " << v[index] + 1 << endl;
  }
}
