#include <bits/stdc++.h>
using namespace std;
int loc[100010];
int main() {
  int prec(0);
  int modif(0);
  int x = -1, y = -1;
  int n, c;
  cin >> n;
  for (int i(1); i <= n; i++) {
    cin >> c;
    loc[c] = i;
  }
  for (int i(1); i <= n; i++) {
    if (abs(prec - loc[i]) != 1) {
      modif++;
      if (x == -1)
        x = i;
      else if (y == -1)
        y = i;
      else {
        cout << "0 0";
        return 0;
      }
    }
    prec = loc[i];
  }
  if (x == -1) {
    cout << "0 0";
    return 0;
  }
  if (y == -1) {
    if (loc[1] == 1)
      cout << x << ' ' << n;
    else if (x == 1)
      cout << "1 " << n;
    else
      cout << "1 " << x;
  } else {
    for (int i(1); i < x; i++) {
      if (loc[i] != i) {
        cout << "0 0";
        return 0;
      }
    }
    for (int i(y + 1); i <= n; i++) {
      if (loc[i] != i) {
        cout << "0 0";
        return 0;
      }
    }
    cout << x << ' ' << y - 1;
  }
  return 0;
}
