#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j, val, l = -1, r = -1;
  int flag = 0;
  cin >> n;
  vector<int> V(n);
  for (i = 0; i < n; i++) cin >> V[i];
  for (i = 0; i < n; i++) {
    if (i + 1 != V[i]) {
      l = i + 1;
      r = V[i];
      reverse(V.begin() + i, V.begin() + V[i]);
      for (; i < n; i++) {
        if (V[i] != i + 1) {
          l = 0;
          r = 0;
        }
        if (!l) break;
      }
    }
    if (l != -1) break;
  }
  if (l == -1)
    cout << "0 0";
  else
    cout << l << " " << r;
  return 0;
}
