#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j, r = -1, l = -1;
  cin >> n;
  int a[n + 1];
  for (i = 1; i <= n; i++) cin >> a[i];
  int x = 1;
  for (i = 1; i <= n; i++) {
    if (a[i] == i)
      continue;
    else if (r == -1 && a[i] != i)
      r = a[i], l = i;
    else if (i <= r && r != -1) {
      if (a[i] != a[i - 1] - 1) {
        x = 0;
        break;
      }
    } else if (i > r && r != -1) {
      if (a[i] != i) {
        x = 0;
        break;
      }
    }
  }
  if (r == -1 && l == -1) x = 0;
  x == 1 ? cout << l << " " << r : cout << 0 << " " << 0;
  return 0;
}
