#include <bits/stdc++.h>
int main() {
  int n;
  std::cin >> n;
  std::vector<int> v(n + 1);
  for (int i = 1; i <= n; ++i) {
    std::cin >> v[i];
  }
  int left = 0;
  if (v[1] != 1) {
    left = 1;
  } else {
    for (int i = 2; i <= n; ++i) {
      if (v[i] != v[i - 1] + 1) {
        left = i;
        break;
      }
    }
  }
  if (left > 0) {
    int right = 0;
    if (v[n] != n) {
      right = n;
    } else {
      for (int i = n - 1; i > right; --i) {
        if (v[i] != v[i + 1] - 1) {
          right = i;
          break;
        }
      }
    }
    int i;
    for (i = left; i < right; ++i) {
      if (v[i] != v[i + 1] + 1) {
        break;
      }
    }
    if (i == right) {
      std::cout << left << " " << right << std::endl;
      return 0;
    }
  }
  std::cout << "0 0" << std::endl;
}
