#include <bits/stdc++.h>
int main() {
  int b = 1, n, i, k, kn, k0, m[1000];
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%d", &m[i]);
  i = 0;
  while ((i < n) && (m[i] == i + 1)) i++;
  if (i == n) b = 0;
  k = m[i];
  kn = i + 1;
  k0 = m[i];
  if (b == 1)
    while (i < k - 1) {
      i++;
      k0--;
      if (m[i] != k0) {
        b = 0;
        break;
      }
    }
  for (i = k; i < n; i++)
    if (m[i] != i + 1) b = 0;
  if (b == 0)
    printf("0 0");
  else
    printf("%d %d", kn, k);
  return 0;
}
