#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
template <typename T>
inline bool remin(T& c, const T& n) {
  if (c > n) {
    c = n;
    return 1;
  }
  return 0;
}
template <typename T>
inline bool remax(T& c, const T& n) {
  if (c < n) {
    c = n;
    return 1;
  }
  return 0;
}
int _in;
int in() {
  scanf("%d", &_in);
  return _in;
}
int main() {
  int i, j, t, n;
  scanf("%d", &n);
  vector<int> maj(n);
  for ((i) = (0); (i) < (n); ++(i)) maj[i] = in();
  for ((i) = (0); (i) < (n); ++(i))
    if (maj[i] != i + 1) break;
  if (i == n) {
    puts("0 0");
    return 0;
  }
  for (j = n; --j >= 0 && maj[j] == j + 1;)
    ;
  for (t = i; t < j; ++t)
    if (maj[t] - 1 != maj[t + 1]) {
      puts("0 0");
      return 0;
    }
  printf("%d %d\n", i + 1, j + 1);
  return 0;
}
