#include <bits/stdc++.h>
using namespace std;
const double pi = 3.1415926535897932;
const long long MAXN = 1000000;
const long long base = 1000000000;
#pragma comment(linker, "/STACK:64000000")
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int s = 0;
  for (int i = 0; i < n; i++)
    if (a[i] == i + 1) s++;
  if (s == n) {
    cout << "0 0";
    return 0;
  }
  int u1 = -1, u2 = -1;
  for (int i = 0; u1 == -1; i++) {
    if (a[i] != i + 1) u1 = i;
  }
  for (int i = n - 1; u2 == -1; i--) {
    if (a[i] != i + 1) u2 = i;
  }
  reverse(a.begin() + u1, a.begin() + u2 + 1);
  s = 0;
  for (int i = 0; i < n; i++)
    if (a[i] == i + 1) s++;
  if (s < n)
    cout << "0 0";
  else
    cout << u1 + 1 << ' ' << u2 + 1;
  return 0;
}
