Vasya collects coins: he has exactly one coin for every year from 1 to n. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from l to r inclusively and put them in the reverse order. That is, he took a certain segment [l, r] and reversed it. At that the segment's endpoints did not coincide. For example, if n = 8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2, 6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... n using exactly one segment reversal. If it is possible, find the segment itself.

Input

The first line contains an integer n (1 ≤ n ≤ 1000) which is the number of coins in Vasya's collection. The second line contains space-separated n integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to n, and every number is used exactly 1 time.

Output

If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers l r (1 ≤ l < r ≤ n) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... n the given one.

Examples

Input

8
1 6 5 4 3 2 7 8


Output

2 6


Input

4
2 3 4 1


Output

0 0


Input

4
1 2 3 4


Output

0 0

Source: codeforces | Difficulty: EASY
Tags: Implementation


Solutions:
n = int(input())
l = list(map(int, input().split()))
v = l.copy()
v.sort()
i = 0
j = 0
l1 = []
l2 = []
ans = []
while i < n and j < n:
	if l[i] == v[j]:
		i += 1
		j += 1
	else:
		l1.append(v[j])
		l2.append(l[i])
		ans.append(j)
		j += 1
		i += 1
l2.reverse()
if l1 == l2 and len(ans) >= 2:
	print(ans[0] + 1, ans[-1] + 1)
else:
	print(0, 0)


n = int(input())
k = [0] + list(map(int, input().split()))
for i in range(1, n + 1):
	if i != k[i]:
		t = k[i]
		k[i:t + 1] = k[i:t + 1][::-1]
		if k == [*range(n + 1)]:
			print(i, t)
		else:
			print(0, 0)
		exit()
print(0, 0)


n = int(input())
A = list(map(int, input().split()))
T = list(range(1, n + 1))
for (i, a) in enumerate(A):
	if a != i + 1:
		break
else:
	print(0, 0)
	exit()
B = A[0:i] + list(reversed(A[i:a])) + A[a:]
if B == T:
	print(i + 1, a)
else:
	print(0, 0)


(n, t) = (int(input()), list(map(int, input().split())))
(a, b) = (0, n)
while a < n and t[a] == a + 1:
	a += 1
if a == n:
	print('0 0')
else:
	while b > 0 and t[b - 1] == b:
		b -= 1
	print(str(a + 1) + ' ' + str(b) if t[a:b] == list(range(b, a, -1)) else '0 0')


n = int(input())
S = str(input())
l = S.split(' ')
M = '0 0'
f = 0
L = [int(i) for i in l]
for i in range(n):
	if L[i] != i + 1 and f == 0:
		j = L[i]
		if L[j:] != [i for i in range(j + 1, n + 1)]:
			print('0 0')
			f = 2
			break
		else:
			M = str(i + 1) + ' ' + str(j)
			f = 1
	if L[L[i] - 1] != i + 1:
		print('0 0')
		f = 2
		break
if f != 2:
	print(M)


import sys
n = int(input())
l = list(map(int, input().split()))
l1 = l.copy()
l1.sort()
if n <= 2:
	if l == l1:
		print(0, 0)
	else:
		print(1, 2)
	sys.exit()
for i in l:
	if i == 1:
		break
	else:
		l.remove(i)
		l.append(i)
if l == l1:
	print(0, 0)
else:
	for i in range(n - 1):
		if abs(l[i] - l[i + 1]) > 1:
			a = i + 1 + 1
			b = l[i + 1]
			l2 = l[:i + 1]
			l3 = l[i + 1:b]
			l4 = l[b:]
			l3 = l3[::-1]
			l5 = l2 + l3 + l4
			break
	if l5 == l1:
		print(a, b)
	else:
		print(0, 0)


(n, a) = (int(input()), [0] + list(map(int, input().split())))
(l, r) = (1, n)
while l <= n and a[l] == l:
	l += 1
while r >= 1 and a[r] == r:
	r -= 1
if l < n and a[l:r + 1] == list(range(r, l - 1, -1)):
	print(l, r)
else:
	print('0 0')


n = int(input())
a = list(map(int, input().split()))
(l, r) = (-1, -1)
for i in range(n):
	if a[i] != i + 1:
		l = i
		break
for i in range(n - 1, -1, -1):
	if a[i] != i + 1:
		r = i
		break
j = r + 1
for i in range(l, r + 1):
	if a[i] == j:
		j -= 1
		continue
	else:
		print(0, 0)
		exit(0)
print(l + 1, r + 1)


def main():
	num = int(input())
	data = [int(x) for x in input().split()]
	left = 0
	right = 0
	sign = True
	i = 0
	j = 0
	if len(data) < 2:
		sign = False
	for i in range(len(data)):
		if data[i] != i + 1:
			break
	left = i + 1
	if left > num:
		sign = False
	for j in range(num - 1, i, -1):
		if data[j] != j + 1:
			break
	right = j + 1
	if right < 1:
		sign = False
	if left > right:
		sign = False
	for i in range(left - 1, right - 1):
		if data[i] != data[i + 1] + 1:
			sign = False
			break
	if sign:
		print(left, right)
	else:
		print('0 0\n')
main()


n = int(input())
s = []
for i in range(1, n + 1):
	s.append(i)
a = list(map(int, input().split()))
l = -1
r = n - 1
for i in range(n):
	if i + 1 != a[i] and l == -1:
		l = i
		break
for i in range(n - 1, l, -1):
	if i + 1 != a[i] and l != -1:
		r = i
		break
b = a[::-1]
if s == b and s != a:
	print(1, n)
elif s == a[:l] + a[r:l - 1:-1] + a[r + 1:] and s != a:
	print(l + 1, r + 1)
else:
	print(0, 0)


n = int(input())
list_str = input().split(' ')
list = [int(l) for l in list_str]
phase = 1
droite = 0
gauche = 0
aux = True
for i in range(-1, n):
	if i == -1:
		g = 0
		d = list[i + 1]
	elif i == n - 1:
		g = list[i]
		d = n + 1
	else:
		g = list[i]
		d = list[i + 1]
	if phase == 1:
		if g + 1 < d:
			phase = 2
			droite = d
		elif g + 1 > d:
			aux = False
			break
	elif phase == 2:
		if g - 1 < d:
			phase = 3
			gauche = g
		elif g - 1 > d:
			aux = False
			break
	elif phase == 3:
		if g + 1 != d:
			aux = False
			break
if aux:
	print(str(gauche) + ' ' + str(droite))
else:
	print('0 0')


n = int(input())
l = [int(i) for i in input().split()]
f = 0
if l == sorted(l):
	print('0 0')
	exit()
if len(set(l)) != n:
	print('0 0')
	exit()
f = 0
for i in range(0, n):
	if l[i] != i + 1:
		f = 1
		indx = l.index(i + 1)
		curr = i
		arr = l[i:indx + 1]
		arr = arr[::-1]

		def check(arr):
			n = len(arr)
			for i in range(1, n):
				if arr[i] != arr[i - 1] + 1:
					return 0
			return 1
		if check(arr) and l[indx + 1:] == sorted(l[indx + 1:]):
			print(curr + 1, indx + 1)
		else:
			print('0 0')
		exit()
print('0 0')


from sys import stdin, stdout
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
(n, arr) = (ii1(), iia())
p1 = p2 = 0
st = False
for i in range(1, n):
	if arr[i] < arr[i - 1] and (not st):
		st = True
		p1 = i - 1
	if st and arr[i] > arr[i - 1]:
		p2 = i - 1
		break
if st and p1 >= p2:
	p2 = n - 1
arr = arr[:p1] + arr[p1:p2 + 1][::-1] + arr[p2 + 1:]
if arr == sorted(arr) and st:
	print(p1 + 1, p2 + 1)
else:
	print(0, 0)


def spoilt(arr):
	arr = [0] + arr
	ans = [0, 0]
	cnt = 0
	temp = 0
	flag = False
	for i in range(1, len(arr)):
		if arr[i] != i and flag == False:
			ans[0] = i
			temp = i
			flag = True
		if arr[i] == temp:
			ans[1] = i
			break
	arr = arr[:ans[0]] + arr[ans[0]:ans[1] + 1][::-1] + arr[ans[1] + 1:]
	if arr != sorted(arr):
		print(0, 0)
		return ''
	print(*ans)
	return ''
a = int(input())
lst = list(map(int, input().strip().split()))
print(spoilt(lst))


n = int(input())
A = list(map(int, input().split()))
B = A.copy()
B.sort()
left = 0
right = 0
i = 0
c = 0
flag = 0
while i < n:
	if A[i] != B[i]:
		left = B[i]
		right = A[i]
		if A[left - 1:right] == B[left - 1:right][::-1]:
			i = right - 1
			c += 1
		else:
			flag = 1
			break
	i += 1
if flag == 0 and c == 1:
	print(left, right)
else:
	print(0, 0)


n = int(input())
liste = input()
liste = liste.split(' ')
p = []
for i in range(n):
	p.append(int(liste[i]))
correct = []
perm = []
for i in range(n):
	if p[i] != i + 1:
		perm.append(p[i])
	elif i > 0 and p[i] == i + 1 and (p[i] == p[i - 1] - 1):
		perm.append(p[i])
	else:
		correct.append(p[i])
if perm:
	max = perm[0]
	min = perm[-1]
	m = len(perm)
	liste = [0] * m
	booleen = False
	for i in range(m):
		liste[i] = perm[m - 1 - i]
	if liste == list(range(min, max + 1)):
		print(min, max)
	else:
		print('0 0')
else:
	print('0 0')


n = int(input())
a = list(map(int, input().split()))
if a == [i for i in range(1, n + 1)]:
	print(0, 0)
else:
	if len(a) == 2:
		print(1, 2)
		exit()
	c = []
	for i in range(len(a) - 1):
		if a[i] == a[i + 1] + 1:
			c.append(i)
	if len(c) != 0:
		c.append(c[-1] + 1)
		l = c[0] + 1
		r = c[-1] + 1
		p = []
		p += a[:l - 1]
		p += a[r - 1:l - 2:-1]
		p += a[r:]
		if p == [i for i in range(1, n + 1)]:
			print(l, r)
		else:
			print(0, 0)
	else:
		print(0, 0)


n = int(input())
a = list(map(int, input().split(' ')))
fl1 = False
fl2 = False
b = a[0]
t1 = 0
t2 = n
d = []
fl3 = True
for i in range(len(a)):
	if a[i] < b:
		t1 = i
		fl1 = True
		break
	b = a[i]
if fl1:
	b = a[t1]
	for i in range(t1 + 1, n):
		if a[i] > b:
			t2 = i
			fl2 = True
			break
		b = a[i]
if fl1:
	b = 0
	for i in range(t1 - 1):
		d.append(a[i])
	for i in range(t2 - 1, t1 - 2, -1):
		d.append(a[i])
	for i in range(t2, n):
		d.append(a[i])
	for i in range(n):
		if d[i] < b:
			fl3 = False
			break
		b = d[i]
	if fl3:
		print(t1, t2)
	else:
		print('0 0')
else:
	print('0 0')


def f():
	n = int(input())
	L = input().split()
	etat = 0
	(debut, fin) = (0, 0)
	for i in range(n):
		num = int(L[i])
		if etat == 1:
			if 1 + i + num != debut + fin:
				return '0 0'
			elif i + 1 == fin:
				etat = 2
		elif i + 1 != num:
			if etat == 0:
				etat = 1
				debut = i + 1
				fin = num
			elif etat == 2:
				return '0 0'
	return str(debut) + ' ' + str(fin)
print(f())


n = int(input())
a = list(map(int, input().split()))
(l, r) = (-1, -1)
for i in range(n):
	if a[i] != i + 1:
		l = i
		break
for i in range(n - 1, -1, -1):
	if a[i] != i + 1:
		r = i
		break
if l == r or a[r] > a[l]:
	print(0, 0)
else:
	possible = True
	for i in range(l, r + 1):
		if a[i] != a[l] - (i - l):
			possible = False
	if possible:
		print(l + 1, r + 1)
	else:
		print(0, 0)


n = int(input())
arr = list(map(int, input().split()))
s = -1
e = -1
for i in range(n):
	if arr[i] != i + 1:
		s = i
		e = arr[i] - 1
		break
new_arr = arr[:s] + list(reversed(arr[s:e + 1])) + arr[e + 1:]
if new_arr == sorted(arr) and s != -1:
	print(s + 1, e + 1)
else:
	print(0, 0)


n = int(input())
a = list(map(int, input().split()))
s = sorted(a)
l = 0
r = 0
c = 0
b = 0
for i in range(1, n):
	if a[i] < a[i - 1]:
		l = i - 1 if c == 0 else l
		c = 1
	elif c == 1:
		r = i
		b = 1
		break
if b == 0 and c == 1:
	r = n
a1 = a[l:r]
a1.reverse()
a = a[:l] + a1 + a[r:]
if a == s and c:
	print(l + 1, r)
else:
	print(0, 0)


def res():
	n = int(input())
	v = [int(i) for i in input().split()]
	ans = sorted(v)
	if v == ans:
		return '0 0'
	try:
		ini = 0
		temp = []
		while v[ini] < v[ini + 1]:
			temp.append(v[ini])
			ini += 1
		fim = ini
		v2 = []
		while fim < n - 1 and v[fim] > v[fim + 1]:
			v2.append(v[fim])
			fim += 1
		v2.append(v[fim])
		ini += 1
		fim += 1
		temp += v2[::-1] + v[fim:]
		if temp == ans:
			return str(ini) + ' ' + str(fim)
		else:
			return '0 0'
	except:
		return '0 0'

def main():
	print(res())
main()


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
pos = []
i = 0
while i < n and a[i] == i + 1:
	i += 1
if i == n:
	print(0, 0)
else:
	pos.append(i + 1)
	sn = a[i]
	j = 0
	while i < n and a[i] == sn - j:
		i += 1
		j += 1
	pos.append(i)
	j = 1
	while i < n:
		if a[i] != sn + j:
			print(0, 0)
			break
		i += 1
		j += 1
	else:
		print(pos[0], pos[1])


n = int(input())
arr = [0] + list(map(int, input().split()))
(l, r) = (-1, -1)
i = 0
while i < n and arr[i] + 1 == arr[i + 1]:
	i += 1
l = i
i += 1
while i < n and arr[i] - 1 == arr[i + 1]:
	i += 1
	r = i
i += 1
while i < n and arr[i] + 1 == arr[i + 1]:
	i += 1
if r != -1 and i >= n - 1:
	print(l + 1, r)
else:
	print(0, 0)


def ascending_until(l):
	for (k, e) in enumerate(l):
		if k + 1 != e:
			return k
	return len(l)

def descending_until(l):
	for (k, e) in enumerate(reversed(l)):
		if len(l) - k != e:
			return len(l) - k
	return 0
n = int(input())
order = list(map(int, input().split()))
i = ascending_until(order)
if i == len(order):
	print(0, 0)
else:
	j = descending_until(order)
	order[i:j] = list(reversed(order[i:j]))
	if ascending_until(order) == len(order):
		print(order[i], order[j - 1])
	else:
		print(0, 0)


n = int(input())
list1 = list(map(int, input().split()))
list2 = list1.copy()
list2.sort()
ll = []
x = 0
y = 0
for i in range(n):
	if list1[i] != i + 1:
		x = i + 1
		break
for i in range(n - 1, -1, -1):
	if list1[i] != i + 1:
		y = i + 1
		break
if x == 0 and y == 0:
	print(x, y)
else:
	list1 = list1[0:x - 1] + list((list1[i] for i in range(y - 1, x - 2, -1))) + list1[y:]
	if list1 == list2:
		print(x, y)
	else:
		print(0, 0)


def s():
	input()
	a = list(map(int, input().split()))
	b = list(range(1, len(a) + 1))
	(l, r) = (0, 0)
	for (i, v) in enumerate(a):
		if v != b[i]:
			l = i
			break
	for (i, v) in reversed(list(enumerate(a))):
		if v != b[i]:
			r = i
			break
	a[l:r + 1] = reversed(a[l:r + 1])
	if l != r and a == b:
		print(l + 1, r + 1)
	else:
		print(0, 0)
s()


r = range
i = input
i()
l = list(map(int, i().split()))
s = 0
p = q = -1
for _ in r(len(l)):
	if l[_] != _ + 1:
		p = _ + 1
		q = l[_]
		break
if p != -1:
	l[p - 1:q] = sorted(l[p - 1:q])
f = 1 * (p != -1)
for x in r(len(l)):
	if l[x] != x + 1:
		f = 0
if f:
	print('%d %d' % (p, q))
else:
	print('0 0')


n = int(input())
l = list(map(int, input().split()))
k = 1
while k < n + 1 and l[k - 1] == k:
	k += 1
else:
	if k == n + 1:
		print(0, 0)
	else:
		start = k
		curr_sum = k + l[k - 1]
		while k < n + 1 and k + l[k - 1] == curr_sum:
			k += 1
		if k + start != curr_sum + 1:
			print(0, 0)
		else:
			end = k - 1
			while k < n + 1 and l[k - 1] == k:
				k += 1
			else:
				if k < n + 1:
					print(0, 0)
				else:
					print(start, end)


n = int(input())
seg = [int(s) - 1 for s in input().split()]
r = -1
l = n
etape = 1
prec = -1
for (k, i) in enumerate(seg):
	if not i == prec + etape:
		etape = -etape
		if r == -1:
			r = k + 1
		elif l == n:
			l = k
		else:
			print(0, 0)
			exit()
	prec = i
if r == -1:
	print(0, 0)
	exit()
print(r, l)


import os, sys
from io import IOBase, BytesIO
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log, pi, sin
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var, end='\n'):
	sys.stdout.write(str(var) + end)

def outarr(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]
n = int(data())
arr = l()
if n == 1:
	out('0 0')
	exit()
temp = arr[:]
temp.sort()
if temp == arr[::-1]:
	out('1 ' + str(n))
	exit()
(left, right) = (0, 0)
cnt = 0
for i in range(1, n):
	if arr[i] - arr[i - 1] > 1:
		if not left:
			left = i + 1
		elif not right:
			right = i
		else:
			out('0 0')
			exit()
if not right and left:
	right = n
outarr(str(left) + ' ' + str(right))


n = int(input())
arr = list(map(int, input().split()))
ans = True
for i in range(n):
	if i + 1 != arr[i]:
		for j in range(arr[i], n):
			if j + 1 != arr[j]:
				ans = False
				break
		for j in range(i, arr[i]):
			if arr[i] - j + i != arr[j]:
				ans = False
				break
		if ans:
			print(i + 1, arr[i])
			exit(0)
		break
print(0, 0)


a = int(input())
flag = 0
(l, r) = (0, 0)
i = 0
b = [int(i) for i in input().split()]
while i < a:
	if b[i] != i + 1:
		if sorted(b[i:b[i]]) != [*range(i + 1, b[i] + 1)]:
			exit(print('0 0'))
		elif flag == 0:
			flag = 1
			l = i + 1
			r = b[i]
			i = b[i]
		else:
			exit(print('0 0'))
	else:
		i += 1
print(l, r)


n = int(input())
a = list(map(int, input().split()))
if a == [2, 1]:
	print(1, 2)
	exit()
L = 0
if a[L] == 1:
	while L < n - 1:
		if a[L + 1] - a[L] != 1:
			break
		L += 1
if L == n - 1:
	print(0, 0)
	exit()
L += 1
R = a.index(L + 1)
a = a[:L] + a[R:L - 1:-1] + a[R + 1:]
b = [x for x in range(1, n + 1)]
if a == b:
	print(L + 1, R + 1)
else:
	print(0, 0)


n = int(input())
a = list(map(int, input().split()))
l = 0
r = 0
i = 0
while i < len(a):
	if i + 1 != a[i]:
		if i + 1 != a[a[i] - 1]:
			break
		if 0 != l and 0 != r:
			l = 0
			r = 0
			break
		sb = False
		for j in range(i, a[i] - 2):
			if a[j] != a[j + 1] + 1:
				sb = True
				break
		if sb:
			break
		l = i + 1
		r = a[i]
		i = r
	i += 1
if 0 != l and 0 != r:
	print(l, r)
else:
	print(0, 0)


n = int(input())
(l, r) = (0, 0)
check = 0
list = list(map(int, input().split()))
i = 0
while i < n:
	if i + 1 != list[i]:
		check += 1
		if check > 1:
			break
		l = i + 1
		while True:
			if i >= n - 1 or list[i] < list[i + 1]:
				i += 1
				break
			i += 1
			r = i + 1
	else:
		i += 1
if check <= 1 and l != r and (l != 0) and (r != 0):
	print(l, r)
else:
	print(0, 0)


n = int(input())
arr = list(map(int, input().split()))
(lft, rgt) = (n, 0)
for i in range(n):
	if arr[i] != i + 1:
		lft = min(lft, i)
		rgt = max(rgt, i)
ok = True
for i in range(lft + 1, rgt + 1):
	if arr[i - 1] < arr[i]:
		ok = False
if ok and rgt - lft + 1 > 1:
	print(lft + 1, rgt + 1)
else:
	print(0, 0)


def crs(v, n, r, l):
	if r <= l:
		return 0
	for c in range(l, r):
		if v[c] - v[c + 1] != 1:
			return 0
	return 1

def gfl(v, n):
	(r, l) = (0, 0)
	(c, b) = (0, 0)
	while c < n:
		if v[c] != c + 1:
			if not crs(v, n, v[c] - 1, c) or b:
				return (0, 0)
			(b, r, l) = (1, v[c], c + 1)
			c = v[c] - 1
		c += 1
	return (l, r)
input()
v = [int(x) for x in input().split()]
(l, r) = gfl(v, len(v))
print(l, r)


n = int(input())
l = list(map(int, input().split()))
l1 = list(range(1, n + 1))
i = 0
L = 0
R = 0
c = 0
while i < n:
	if l[i] != l1[i]:
		if c == 0:
			L = i
			c += 1
		else:
			R = i
	i += 1
if L != 0 or R != 0:
	if l[L:R + 1] != l1[L:R + 1][::-1]:
		L = 0
		R = 0
		print(0, 0)
	else:
		print(L + 1, R + 1)
else:
	print(0, 0)


n = int(input())
L = [int(i) for i in input().split(' ')]

def main(n, L):
	since = -1
	end = -1
	if n == 1:
		return '0 0'
	if n == 2:
		return '0 0' if L[0] < L[1] else '1 2'
	if L[1] == L[0] + 1:
		order = 1
	elif L[1] == L[0] - 1:
		order = -1
		since = 0
	elif L[2] == L[1] - 1:
		order = -1
		since = 1
	else:
		return '0 0'
	for i in range(2, n):
		if L[i] != L[i - 1] + order:
			if order == 1:
				if since == -1:
					order = -1
					since = i
				else:
					return '0 0'
			else:
				end = i - 1
				order = 1
		if end < since and end != -1:
			print(i)
	if end == -1 and since != -1:
		end = n - 1
	since += 1
	end += 1
	if since == end:
		return '0 0'
	return '{} {}'.format(since, end)
print(main(n, L))


n = int(input())
a = list(map(int, input().split()))
desc_start = None
desc_end = None
i = 0
while i < n and a[i] == i + 1:
	i += 1
desc_start = i
while i < n and a[i] == a[desc_start] - (i - desc_start):
	i += 1
desc_end = i
while i < n and a[i] == i + 1:
	i += 1
if i == n and desc_start != desc_end:
	print('%d %d' % (desc_start + 1, desc_end))
else:
	print('0 0')


n = int(input())
(start, end) = (0, 0)
flag = 0
ls = list(map(int, input().split()))
for i in range(1, n + 1):
	if ls[i - 1] != i:
		start = i
		break
for j in range(n, i - 1, -1):
	if ls[j - 1] != j:
		end = j
		break
if start == 0 and end == 0:
	flag = 1
	print('0 0')
else:
	for i in range(start - 1, end - 1):
		if ls[i] < ls[i + 1]:
			print('0 0')
			flag = 1
			break
if flag == 0:
	print(str(start) + ' ' + str(end))


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
mod = int(1000000000.0) + 7
n = iinput()
a = get_list()
i = 0
flag = False
while i < n:
	l = i
	while i < n - 1 and a[i + 1] == a[i] - 1:
		i += 1
	r = i
	li = a[:l] + list(reversed(a[l:r + 1])) + a[r + 1:]
	i += 1
	if li == list(sorted(li)) and l != r:
		print(l + 1, r + 1)
		flag = True
		break
if flag == False:
	print(0, 0)


n = int(input())
L = list(map(int, input().split()))
start = 0
end = n - 1
while L[start] == start + 1:
	start += 1
	if start == n:
		break
while L[end] == end + 1:
	end -= 1
	if end == -1:
		break
if start > end:
	print('0 0')
else:
	pre = L[:start]
	mid = L[start:end + 1]
	post = L[end + 1:]
	if pre + sorted(mid) + post == sorted(L) and mid == sorted(mid, reverse=True):
		print(start + 1, end + 1)
	else:
		print('0 0')


import sys
from array import array

def input():
	return sys.stdin.buffer.readline().decode('utf-8')
n = int(input())
a = list(map(int, input().split()))
(l, r) = (-1, -1)
for i in range(n):
	if a[i] != i + 1:
		l = i
		break
for i in range(n - 1, -1, -1):
	if a[i] != i + 1:
		r = i
		break
a[l:r + 1] = a[l:r + 1][::-1]
if a == list(range(1, n + 1)):
	print(l + 1, r + 1)
else:
	print(0, 0)


n = input('')
n = int(n)
t = input('')
t = t.split(' ')
SP = True
Fin = False
for i in range(n):
	if int(t[i]) != i + 1:
		(k, r) = (i, int(t[i]))
		SP = True
		break
	else:
		SP = False
if SP == True:
	for i in range(0, n - k):
		if int(t[k + i]) != r - i:
			(a, l) = (k + i, r - i)
			Fin = False
			break
		else:
			Fin = True
	if Fin == False:
		for i in range(0, n - a):
			if int(t[a + i]) != r + i + 1:
				SP = False
				break
		if SP:
			print(str(l + 1) + ' ' + str(r))
		else:
			print('0 0')
	else:
		print(str(k + 1) + ' ' + str(r))
else:
	print('0 0')


(n, a) = (int(input()), list(map(int, input().split())))
(l, r) = (n, 0)
for i in range(n):
	if a[i] != i + 1:
		l = min(l, i)
		r = max(r, i)
zhi = True
for i in range(l + 1, r + 1):
	if a[i - 1] < a[i]:
		zhi = False
if zhi and r - l + 1 > 1:
	print(l + 1, r + 1)
else:
	print(0, 0)


n = int(input())
liststr = input().split(' ')
list = [int(l) for l in liststr]
cnt = 1
dovomi = 0
gavali = 0
flag = True
for i in range(-1, n):
	if i == -1:
		g = 0
		d = list[i + 1]
	elif i == n - 1:
		g = list[i]
		d = n + 1
	else:
		g = list[i]
		d = list[i + 1]
	if cnt == 1:
		if g + 1 < d:
			cnt = 2
			dovomi = d
		elif g + 1 > d:
			flag = False
			break
	elif cnt == 2:
		if g - 1 < d:
			cnt = 3
			gavali = g
		elif g - 1 > d:
			flag = False
			break
	elif cnt == 3:
		if g + 1 != d:
			flag = False
			break
if flag:
	print(str(gavali) + ' ' + str(dovomi))
else:
	print('0 0')


n = int(input())
a = list(map(int, input().split()))
b = a.copy()
b.sort()
i = 0
while i < n and a[i] == b[i]:
	i += 1
lower = i
i = n - 1
while i >= 0 and a[i] == b[i]:
	i -= 1
upper = i
if lower < upper and b[lower:upper + 1] == a[lower:upper + 1][::-1]:
	print(lower + 1, upper + 1)
else:
	print(0, 0)


n = int(input())
a = list(map(int, input().split()))
ans = []
if a == sorted(a):
	print(0, 0)
elif a[::-1] == sorted(a):
	print(1, len(a))
else:
	i = 0
	while i < len(a) - 1:
		if abs(a[i] - a[i + 1]) > 1:
			ans.append(i + 1)
		i += 1
	if len(ans) == 1:
		ans.append(n)
	if a[:ans[0]] + a[ans[0]:ans[1]][::-1] + a[ans[1]:] == sorted(a):
		print(ans[0] + 1, ans[1])
	else:
		print(0, 0)


import math
n = int(input())
lst = list(map(int, input().strip().split(' ')))
l = -1
r = -1
for i in range(n):
	if l == -1 and lst[i] != i + 1:
		l = i
	if r == -1 and lst[n - i - 1] != n - i:
		r = n - i - 1
	if l != -1 and r != -1:
		break
if l == -1 and r == -1:
	print('0 0')
else:
	f = 0
	p = r + 1
	for j in range(l, r + 1):
		if lst[j] == p:
			p -= 1
		else:
			f = 1
			break
	if f == 1:
		print('0 0')
	else:
		print(l + 1, r + 1, end=' ')


n = int(input())
ans = [x for x in range(1, n + 1)]
ar = list(map(int, input().split()))
(f, l) = (n, 1)
for i in range(n):
	if i + 1 != ar[i]:
		f = min(i + 1, f)
		l = i + 1
if f >= l:
	print(0, 0)
else:
	ar = ar[:f - 1] + ar[f - 1:l][::-1] + ar[l:]
	print(str(f) + ' ' + str(l) if ar == ans else '0 0')


import sys
n_nums = int(sys.stdin.readline())
values = [int(x) for x in sys.stdin.readline().split()]
if n_nums == 1:
	print(0, 0)
	sys.exit(0)
if list(reversed(values)) == sorted(values):
	print(1, len(values))
	sys.exit(0)
(start, end) = (False, False)
(s, e) = (0, 0)
for i in range(1, len(values)):
	if values[i] - values[i - 1] < 0:
		if not start:
			start = True
			s = i
	if values[i] - values[i - 1] > 0:
		if start and (not end):
			end = True
			e = i
			break
if start and (not end):
	e = len(values)
in_tune = values[:s - 1] + list(reversed(values[s - 1:e])) + values[e:]
if in_tune == sorted(values):
	print(s, e)
else:
	print(0, 0)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scan_int = lambda : int(input())
scan_string = lambda : input().rstrip()
read_list = lambda : list(read())
read = lambda : map(int, input().split())
read_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(beauty, s, n, count):
	pass
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect

def solve():
	n = scan_int()
	a = read_list()
	start = 0
	end = 0
	for i in range(n):
		if a[i] != i + 1:
			start = i + 1
			break
	for j in range(n - 1, -1, -1):
		if a[j] != j + 1:
			end = j + 1
			break
	if sorted(a[start - 1:end]) != a[start - 1:end][::-1]:
		print(0, 0)
		return
	print(start, end)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(1):
	solve()