#include <bits/stdc++.h>
int get(std::vector<int> &nums, int iter, int upd, int number) {
  while (iter >= 0 && iter < nums.size()) {
    if (nums[iter] == number) {
      iter += upd;
      number += upd;
    } else {
      return iter;
    }
  }
  return -1;
}
int main() {
  int n;
  std::cin >> n;
  std::vector<int> nums;
  for (int iter = 0; iter < n; ++iter) {
    int val;
    std::cin >> val;
    nums.push_back(val);
  }
  int id1 = get(nums, 0, 1, 1), id2 = get(nums, n - 1, -1, n);
  if (id1 == -1) {
    std::cout << "0 0";
    return 0;
  }
  int output = 1, number = id1 + 1;
  for (int iter = id2; iter >= id1; --iter, ++number) {
    if (nums[iter] != number) {
      output = 0;
    }
  }
  if (!output) {
    id1 = id2 = -1;
  }
  std::cout << id1 + 1 << " " << id2 + 1;
  return 0;
}
