#include <bits/stdc++.h>
using namespace std;
bool cmp(string a, string b) { return a + b < b + a; }
string s;
map<string, int> M;
map<string, int>::iterator p;
int main() {
  int a[1005], n, i;
  int x = 1, y;
  cin >> n;
  y = n;
  for (i = 1; i <= n; i++) cin >> a[i];
  while (a[x] == x) x++;
  while (a[y] == y) y--;
  if (x > y)
    printf("0 0");
  else {
    for (i = x; i <= y; i++) {
      if (a[i] != y - (i - x)) break;
    }
    if (i <= y)
      printf("0 0");
    else
      printf("%d %d", x, y);
  }
  return 0;
}
