#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
  int l = -1, r = -1;
  for (int i = 1; i <= n; i++)
    if (v[i] != i) {
      l = i;
      break;
    }
  for (int i = n; i >= 1; i--)
    if (v[i] != i) {
      r = i;
      break;
    }
  if (l == -1 || r == -1) printf("0 0\n"), exit(0);
  for (int i = l; i <= r; i++)
    if (v[i] != r - (i - l)) printf("0 0\n"), exit(0);
  printf("%d %d\n", l, r);
  return 0;
}
