#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
int main() {
  long long int n;
  cin >> n;
  long long int a[n], b[n];
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = i + 1;
  }
  int f = 1;
  for (long long int i = 0; i < n; i++) {
    if (a[i] != b[i]) f = 0;
  }
  if (f) {
    cout << "0 0";
    return 0;
  }
  long long int s = -1, e = -1;
  for (long long int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      s = i;
      if (a[a[i] - 1] != b[i]) {
        cout << "0 0";
        return 0;
      } else {
        e = a[i] - 1;
      }
      break;
    }
  }
  for (long long int i = s; i <= e; i++) {
    swap(a[i], a[a[i] - 1]);
  }
  for (long long int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      cout << "0 0";
      return 0;
    }
  }
  cout << s + 1 << " " << e + 1;
}
