#include <bits/stdc++.h>
using namespace std;
int vis[1005];
int z[1005];
int main() {
  int n;
  while (cin >> n) {
    for (int i = 1; i <= n; i++) {
      cin >> z[i];
      vis[z[i]] = i;
    }
    int sum = 0, flag = 0, ans1, ans2;
    for (int i = 1; i <= n; i++) {
      if (z[i] != i) {
        sum++;
        for (int j = i; j <= z[i]; j++) {
          if (vis[j] != z[j]) {
            flag = 1;
          }
        }
        if (flag == 0) {
          ans1 = i;
          ans2 = z[i];
        }
        i = z[i];
      }
    }
    if (sum == 1 && flag == 0) {
      cout << ans1 << " " << ans2 << endl;
    } else {
      cout << 0 << " " << 0 << endl;
    }
  }
}
