Robot Doc is located in the hall, with n computers stand in a line, numbered from left to right from 1 to n. Each computer contains exactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the i-th of them, the robot needs to collect at least a_{i} any pieces of information from the other computers. Doc can hack the computer only if he is right next to it.

The robot is assembled using modern technologies and can move along the line of computers in either of the two possible directions, but the change of direction requires a large amount of resources from Doc. Tell the minimum number of changes of direction, which the robot will have to make to collect all n parts of information if initially it is next to computer with number 1.

It is guaranteed that there exists at least one sequence of the robot's actions, which leads to the collection of all information. Initially Doc doesn't have any pieces of information.


-----Input-----

The first line contains number n (1 ≤ n ≤ 1000). The second line contains n non-negative integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} < n), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information.


-----Output-----

Print a single number — the minimum number of changes in direction that the robot will have to make in order to collect all n parts of information.


-----Examples-----
Input
3
0 2 0

Output
1

Input
5
4 2 3 0 1

Output
3

Input
7
0 3 1 0 5 2 6

Output
2



-----Note-----

In the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece.

In the second sample to collect all the pieces of information in the optimal manner, Doc can go to the fourth computer and get the piece of information, then go to the fifth computer with one piece and get another one, then go to the second computer in the same manner, then to the third one and finally, to the first one. Changes of direction will take place before moving from the fifth to the second computer, then from the second to the third computer, then from the third to the first computer.

In the third sample the optimal order of collecting parts from computers can look like that: 1->3->4->6->2->5->7.

Difficulty: EASY

Solutions:
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
Power = 0
res = -1
E = [0] * n
while Power != n:
	res += 1
	for i in range(n):
		if E[i] == 0 and Power >= a[i]:
			Power += 1
			E[i] = 1
	if Power != n:
		res += 1
		for i in range(n - 1, -1, -1):
			if E[i] == 0 and Power >= a[i]:
				Power += 1
				E[i] = 1
print(res)


n = int(input())
l = list(map(int, input().split()))
k = 0
k1 = 0
z = 0
t = 1
while k1 < n:
	if t == 1:
		for i in range(n):
			if l[i] <= z and l[i] != -1:
				z = z + 1
				k1 = k1 + 1
				l[i] = -1
	if t == 2:
		for i in range(n - 1, -1, -1):
			if l[i] <= z and l[i] != -1:
				z = z + 1
				k1 = k1 + 1
				l[i] = -1
	if t == 2:
		t = 1
	else:
		t = 2
	if k1 >= n:
		break
	else:
		k = k + 1
print(k)


n = int(input())
l = list(map(int, input().split()))
nl = [-1] * n
keys = 0
turns = 0
while l != nl:
	if turns % 2 == 0:
		for pos in range(n):
			if l[pos] <= keys and l[pos] != -1:
				keys += 1
				l[pos] = -1
	else:
		for pos in range(n - 1, -1, -1):
			if l[pos] <= keys and l[pos] != -1:
				keys += 1
				l[pos] = -1
	turns += 1
print(turns - 1)


def main():
	input()
	(info, dir_c) = (0, -1)
	a = list(map(int, input().split()))
	while a:
		temp = []
		dir_c += 1
		for aa in a:
			if aa <= info:
				info += 1
			else:
				temp.insert(0, aa)
		a = temp
	print(dir_c)

def main2():
	input()
	(kn, res) = (0, -1)
	aa = list(map(int, input().split()))[::-1]
	while aa:
		bb = []
		res += 1
		for a in reversed(aa):
			if a <= kn:
				kn += 1
			else:
				bb.append(a)
		aa = bb
	print(res)
main()


def main():
	input()
	(kn, res) = (0, -1)
	aa = list(map(int, input().split()))[::-1]
	while aa:
		bb = []
		res += 1
		for a in reversed(aa):
			if a <= kn:
				kn += 1
			else:
				bb.append(a)
		aa = bb
	print(res)
main()


n = int(input().split()[0])
s = input().split()
a = [int(i) for i in s]
a = a[::-1]
score = 0
changes = -1
while a:
	changes += 1
	ai = []
	for i in a[::-1]:
		if i > score:
			ai += [i]
		else:
			score += 1
	a = ai[:]
print(changes)


import sys
num_of_comp = sys.stdin.readline()
next_list = [int(i) for i in sys.stdin.readline().split(' ')]
collected_info = 0
change_of_dir = 0
while next_list:
	current_list = list(next_list)
	next_list = list()
	for current_info in current_list:
		if current_info <= collected_info:
			collected_info += 1
		else:
			next_list.insert(0, current_info)
	change_of_dir += 1
print(change_of_dir - 1)


n = int(input())
l = input().split()
l = [int(i) for i in l]
hacked = []
turns = -1
while len(hacked) < n:
	new = []
	for i in range(len(l)):
		if l[i] <= len(hacked):
			new = [i] + new
			hacked.append(i)
	for i in new:
		l.pop(i)
	l.reverse()
	turns += 1
print(turns)


n = int(input())
a = list(map(int, input().split()))
cnt = 0
res = 0
while len(a):
	b = []
	for el in a:
		if el <= cnt:
			cnt += 1
		else:
			b.append(el)
	a = b[::-1]
	res += 1
print(res - 1)


n = int(input())
a = list(map(int, input().split()))
info = 0
i = 0
dir = 1
result = 0
while True:
	if info >= a[i]:
		info += 1
		a[i] = n + 1
	if info == n:
		break
	i += dir
	if i < 0 or i == n:
		dir = -dir
		i += dir
		result += 1
print(result)


n = int(input())
a = list(map(int, input().split()))
turn = 0
info = 0
ok = 1
while ok == 1:
	ok = 0
	if turn % 2 == 0:
		for i in range(0, n):
			if a[i] <= info:
				info += 1
				a[i] = 1001
				ok = 1
	else:
		for i in range(n - 1, -1, -1):
			if a[i] <= info:
				info += 1
				a[i] = 1001
				ok = 1
	if ok == 1:
		turn += 1
print(turn - 1)


n = int(input())
lls = list(map(int, input().split()))
infor = 0
ans = 0
oo = -1
i = 1
j = n - 1
ll = [0] * n
while infor < n:
	if j == oo:
		i = -i
		j = abs(j - n + 1)
		ans += 1
	oo += i
	if infor >= lls[oo] and ll[oo] == 0:
		ll[oo] = 1
		infor += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
used = [False] * n
flag = True
cur = 0
ans = 0
i = 0
while cur < n:
	ans += 1
	if i == 0:
		for i in range(n):
			if not used[i] and a[i] <= cur:
				cur += 1
				used[i] = True
	else:
		for i in range(n - 1, -1, -1):
			if not used[i] and a[i] <= cur:
				cur += 1
				used[i] = True
print(ans - 1)


n = int(input())
a = list(map(int, input().split()))
total = 0
answer = 0
while True:
	for i in range(n):
		if a[i] <= total:
			a[i] = n + 1
			total += 1
	if total == n:
		break
	answer += 1
	for i in range(n - 1, -1, -1):
		if a[i] <= total:
			a[i] = n + 1
			total += 1
	if total == n:
		break
	answer += 1
print(answer)


n = int(input())
a = list(map(int, input().split()))
if n == 1:
	print(0)
elif sorted(a) == a:
	print(0)
else:
	co = 0
	an = -1
	while True:
		if len(set(a)) == 1:
			break
		for i in range(n):
			if a[i] <= co:
				co += 1
				a[i] = 10 ** 5
		an += 1
		if len(set(a)) == 1:
			break
		j = n - 1
		while j >= 0:
			if a[j] <= co:
				co += 1
				a[j] = 10 ** 5
			j -= 1
		an += 1
	print(an)


n = int(input())
a = [int(x) for x in input().split()]
hacked = 0
pos = 0
changes = 0
right = True
while hacked < n:
	if right:
		r = range(pos, n)
	else:
		r = range(pos, -1, -1)
	for i in r:
		if a[i] <= hacked:
			a[i] = n + 1
			hacked += 1
			pos = i
	if hacked < n:
		changes += 1
		right = not right
print(changes)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
import random
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def main():
	n = I()
	a = LI()
	c = 0
	r = -1
	while len(a) > 0:
		r += 1
		b = []
		for k in a:
			if k <= c:
				c += 1
			else:
				b.append(k)
		a = b[::-1]
	return r
print(main())


from sys import stdin
n = int(stdin.readline())
a = [int(x) for x in stdin.readline().split()]
visited = [False] * n
visited_count = 0
turns = 0
dir = 1
position = 0
while True:
	if not visited[position] and a[position] <= visited_count:
		visited[position] = True
		visited_count += 1
	if visited_count == n:
		break
	if dir == 1 and position == n - 1 or (dir == -1 and position == 0):
		dir = -dir
		turns += 1
	position += dir
print(turns)


n = int(input())
a = [int(x) for x in input().split()]
k = 0
t = -1
while a:
	t += 1
	next_a = []
	for elem in a:
		if elem <= k:
			k += 1
		else:
			next_a.append(elem)
	next_a.reverse()
	a = next_a
print(t)


n = int(input())
l = [int(x) for x in input().split()]
done = 0
c = 0
s = set([])
while done < n:
	if c % 2 == 0:
		for i in range(n):
			if done >= l[i] and i not in s:
				done += 1
				s.add(i)
	else:
		for i in range(n - 1, -1, -1):
			if done >= l[i] and i not in s:
				done += 1
				s.add(i)
	if done == n:
		break
	c += 1
print(c)


n = int(input())
l = list(map(int, input().split()))
pieces = 0
res = -1
while pieces < n:
	for i in range(n):
		if l[i] <= pieces:
			pieces += 1
			l[i] = 1001
	res += 1
	if pieces == n:
		print(res)
		exit(0)
	for i in range(n - 1, -1, -1):
		if l[i] <= pieces:
			pieces += 1
			l[i] = 1001
	res += 1
	if pieces == n:
		print(res)
		exit(0)


n = input()
n = int(n.strip())
x = input()
x = list(map(int, x.strip().split()))
informacij = 0
smeri = -1
smer = 'U'
while informacij < n:
	smeri += 1
	if smer == 'U':
		for i in range(len(x)):
			if x[i] is not None and x[i] <= informacij:
				informacij += 1
				x[i] = None
		smer = 'D'
	else:
		for i in range(len(x) - 1, -1, -1):
			if x[i] is not None and x[i] <= informacij:
				informacij += 1
				x[i] = None
		smer = 'U'
print(smeri)


(a, nums, solved, direct, turns) = (input(), list(map(int, input().split())), 0, 1, 0)
while len(nums) != 0:
	if direct == 1:
		removes = []
		for i in range(len(nums)):
			if nums[i] <= solved:
				solved += 1
				removes.append(i)
		for i in removes[::-1]:
			nums.pop(i)
		direct = -1
		turns += 1
	else:
		removes = []
		for i in range(len(nums) - 1, -1, -1):
			if nums[i] <= solved:
				solved += 1
				removes.append(i)
		for i in removes:
			nums.pop(i)
		direct = 1
		turns += 1
print(turns - 1)


n = int(input())
arr = list(map(int, input().split()))
found = 0
ans = -1
cur = 0
while found < n:
	if ans % 2 == 1:
		for i in range(0, len(arr)):
			if arr[i] != -1 and arr[i] <= found:
				found += 1
				arr[i] = -1
		ans += 1
	else:
		for i in range(len(arr) - 1, -1, -1):
			if arr[i] != -1 and arr[i] <= found:
				found += 1
				arr[i] = -1
		ans += 1
print(ans)


x = int(input())
y = [int(num) for num in input().split(' ')]
(cur, dir) = (0, 1)

def solve(a, cur):
	if a == 1:
		for i in range(x):
			if y[i] < cur + 1 and y[i] != -1:
				y[i] = -1
				cur += 1
	else:
		for i in range(x - 1, -1, -1):
			if y[i] < cur + 1 and y[i] != -1:
				y[i] = -1
				cur += 1
	return cur
ret = -1
while cur < x:
	cur = solve(dir, cur)
	dir = 0 - dir
	ret += 1
print(ret)


from math import sqrt, pow, log, log2, log10, exp
from copy import deepcopy

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

def read_int():
	return read_ints()[0]

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

def read_float():
	return read_floats()[0]

def one_dim_array(value, n):
	return [deepcopy(value) for x in range(n)]

def two_dim_array(value, n, m):
	return [[deepcopy(value) for x in range(m)] for x in range(n)]

def is_prime(n):
	if n == 2:
		return True
	if n % 2 == 0:
		return False
	for i in range(3, sqrt(n) + 1):
		if n % i == 0:
			return False
	return True

def max_len_sublist(l, f):
	(start, max_length, length) = (0, 0, 0)
	for i in range(1, len(l)):
		if f(l[i], l[i - 1]):
			length += 1
		else:
			if max_length < length:
				start = i - length
				max_length = length
			length = 0
	return (start, max_length)

def tf_to_yn(b):
	return 'YES' if b else 'NO'
n = read_int()
c = read_ints()
v = set()
cnt = 0
t = 0
while len(v) != n:
	for i in range(n):
		if c[i] <= cnt and i not in v:
			cnt += 1
			v.add(i)
	if len(v) == n:
		break
	t += 1
	for i in range(n - 1, -1, -1):
		if c[i] <= cnt and i not in v:
			cnt += 1
			v.add(i)
	if len(v) == n:
		break
	t += 1
print(t)


n = int(input())
l = input().split()
for i in range(n):
	l[i] = int(l[i])
i = 0
k = 0
r = 0
s = 1
p = 0
while k != n:
	if k >= l[i] and l[i] != -1:
		k += 1
		l[i] = -1
	if i == n - 1 or (i == 0 and p != 0):
		if k == n:
			break
		r += 1
		s = s * -1
		p = 1
	i += s
print(r)


import sys

def solve(n, an):
	ans = 0
	got = 0
	while True:
		for i in range(n):
			if an[i] <= got:
				an[i] = n + 1
				got += 1
		if got == n:
			break
		ans += 1
		i = n - 1
		while i >= 0:
			if an[i] <= got:
				an[i] = n + 1
				got += 1
			i -= 1
		if got == n:
			break
		ans += 1
	return ans
n = int(input())
an = list(map(int, sys.stdin.readline().split()))
print(solve(n, an))


n = int(input())
a = [int(x) for x in input().split()]
collected = 0
ans = -1
dr = 1
st = -1
while collected < n:
	st += dr
	ans += 1
	while st >= 0 and st < n:
		if a[st] != -1 and a[st] <= collected:
			collected += 1
			a[st] = -1
		st += dr
	dr *= -1
print(ans)


__author__ = 'yarsanich'
n = int(input(''))
a = list(map(int, input().split()))
position = 0
inf = 0
ans_r = 0
step = 1
vector = 0
while inf != n:
	if a[position] <= inf:
		inf += 1
		a[position] += 1000000
	if inf != n and (position == n - 1 or (position == 0 and vector)):
		ans_r += 1
		vector = 1
		step *= -1
	position += step
print(ans_r)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	cur = 0
	j = 1
	cnt = 0
	i = 0
	used = [False] * n
	while True:
		if used == [True] * n:
			break
		while 0 <= i < n and (a[i] > cur or (a[i] <= cur and used[i])):
			i += j
		if i == -1 or i == n:
			j = -j
			cnt += 1
			i = 0 if i == -1 else n - 1
		else:
			used[i] = True
			cur += 1
	print(cnt)
main()


n = int(input())
a = list(map(int, input().split(' ')[:n]))
n_infos = 0
direction = 1
res = 0
while True:
	if direction == 1:
		r = list(range(n))
	else:
		r = list(range(n - 1, -1, -1))
	for i in r:
		if n_infos >= a[i]:
			a[i] = float('inf')
			n_infos += 1
	if n_infos == n:
		break
	direction = -direction
	res += 1
print(res)


changesInDirection = -1
numberOfInformation = 0
n = int(input())
informations = [int(x) for x in input().split()]
while len(informations) > 0:
	indices = []
	for (index, information) in enumerate(informations):
		if information <= numberOfInformation:
			indices.append(index)
			numberOfInformation += 1
	for (doubleindex, index) in enumerate(indices):
		informations.pop(index - doubleindex)
	changesInDirection += 1
	informations = list(reversed(informations))
print(changesInDirection)


fu = input()
s = input().split(' ')
vet = []
for i in s:
	vet.append(int(i))
resp = 0
atual = 0
while True:
	l = []
	for i in vet:
		if i <= atual:
			atual += 1
		else:
			l.append(i)
	if len(l) == 0:
		break
	vet = reversed(l)
	resp += 1
print(resp)


n = int(input())
arr = list(map(int, input().split()))
info = 0
num = 0
count = -1
while num != n:
	count += 1
	for i in range(n):
		if arr[i] >= 0 and arr[i] <= info:
			arr[i] = -1
			info += 1
			num += 1
	arr.reverse()
print(count)


n = int(input())
l = [int(c) for c in input().split()]
count = 0
i = 0
direc = 1
change = 0
while count < n:
	v = l[i]
	if v <= count:
		l[i] = n + 1
		count += 1
	if count == n:
		break
	if i == 0 and direc == -1 or (i == n - 1 and direc == 1):
		direc = -direc
		change += 1
	i += direc
print(change)


_ = input()
ns = map(int, input().split())
c = 0
cur = 0
while True:
	new = []
	for i in ns:
		if i <= cur:
			cur += 1
		else:
			new.append(i)
	if not new:
		break
	ns = reversed(new)
	c += 1
print(c)


(n, A) = (int(input()), list(enumerate(map(int, input().split()))))
(pieces, turns) = (set(), -1)
while len(pieces) < n:
	for (i, a) in A:
		if a <= len(pieces):
			pieces.add(i)
	(A, turns) = (A[::-1], turns + 1)
print(turns)


n = int(input())
a = list(map(int, input().split()))
(res, sum) = (0, 0)
while True:
	for i in range(n):
		if a[i] <= sum:
			sum += 1
			a[i] = 10 ** 10
	a = a[::-1]
	if sum == n:
		break
	else:
		res += 1
print(res)


from sys import stdin, stdout
from collections import Counter

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

def ei():
	return map(int, input().split())

def ip():
	return int(stdin.readline())

def op(ans):
	return stdout.write(str(ans) + '\n')
n = ip()
li = ai()
res = 0
s = 0
while 1:
	for i in range(n):
		if li[i] <= s:
			s += 1
			li[i] = n + 1
	if s == n:
		break
	res += 1
	for i in range(n - 1, -1, -1):
		if li[i] <= s:
			s += 1
			li[i] = n + 1
	if s == n:
		break
	res += 1
op(res)


n = int(input())
c = [int(x) for x in input().split(' ')]
d = 1
count = 0
turns = 0
while count < n:
	rng = range(len(c))
	if d == -1:
		rng = range(len(c) - 1, -1, -1)
	for indx in rng:
		item = c[indx]
		if item != -1 and item <= count:
			count += 1
			c[indx] = -1
	d = d * -1
	if count < n:
		turns += 1
print(turns)


direction = 0
n = int(input())
a = list(map(int, input().split()))
dirs = (range(n), range(n - 1, -1, -1))
pieces = 0
notcracked = n
turns = 0
while notcracked > 0:
	for i in dirs[direction]:
		if a[i] != None and pieces >= a[i]:
			notcracked -= 1
			a[i] = None
			pieces += 1
	direction = 1 - direction
	turns += 1
print(turns - 1)


(n, i, d, nc, nt) = (int(input()), 0, 1, 0, 0)
c = [False] * n
a = list(map(int, input().split()))
while True:
	if not c[i] and nc >= a[i]:
		c[i] = True
		nc += 1
		if nc == n:
			print(nt)
			break
	i += d
	if i < 0 or i >= n:
		d = -d
		i += 2 * d
		nt += 1


def check(list):
	for i in list:
		if i != False:
			return True
	return False
n = int(input())
a = list(map(int, input().split()))
info = 0
dir = 1
ch = 0
b = [True] * n
while check(b):
	if dir == 1:
		co = 0
		for i in range(n):
			if a[i] <= info and b[i]:
				info += 1
				co += 1
				b[i] = False
		if check(b):
			dir = 2
			ch += 1
	elif dir == 2:
		for i in range(n):
			if a[n - i - 1] <= info and b[n - i - 1]:
				info += 1
				b[n - i - 1] = False
		if check(b):
			dir = 1
			ch += 1
print(ch)


n = int(input())
l = list(map(int, input().split()))
info = 0
num = 0
ans = -1
while num != n:
	ans += 1
	for i in range(n):
		if l[i] >= 0 and l[i] <= info:
			l[i] = -1
			info += 1
			num += 1
	l = l[::-1]
print(ans)


n = int(input())
L = list(map(int, input().split()))
P = [0 for _ in range(n)]
r = 0
t = 0
while t < n:
	for k in range(n):
		if P[k] == 0 and L[k] <= t:
			t += 1
			P[k] = 1
	r += 1
	if t == n:
		break
	for k in range(n):
		if P[n - k - 1] == 0 and L[n - k - 1] <= t:
			t += 1
			P[n - k - 1] = 1
	r += 1
print(r - 1)


n = int(input())
(pow, dir) = (0, -1)
cm = [0] * n
p = list(map(int, input().split()))
while pow != n:
	dir += 1
	for i in range(n):
		if cm[i] == 0 and pow >= p[i]:
			pow += 1
			cm[i] = 1
	if pow == n:
		break
	dir += 1
	for i in range(n - 1, -1, -1):
		if cm[i] == 0 and pow >= p[i]:
			pow += 1
			cm[i] = 1
print(dir)


n = int(input())
a = list(map(int, input().split()))
visit = [0] * n
data = 0
route = 'r'
coordinate = 0
ans = 0
while data != n:
	if route == 'r':
		for i in range(coordinate, n):
			if a[i] <= data and (not visit[i]):
				data += 1
				visit[i] = 1
		if data != n:
			coordinate = n - 1
			route = 'l'
			ans += 1
	else:
		for i in range(coordinate, -1, -1):
			if a[i] <= data and (not visit[i]):
				data += 1
				visit[i] = 1
		if data != n:
			coordinate = 0
			route = 'r'
			ans += 1
print(ans)


import time
n = int(input())
A = [int(i) for i in input().split()]
start = time.time()
t = 0
now = 0
ans = -1
while t < n:
	ans += 1
	for i in range(n):
		if A[i] >= 0 and A[i] <= t:
			t += 1
			A[i] = -1
	if t == n:
		break
	ans += 1
	for i in range(n):
		if A[n - 1 - i] >= 0 and A[n - 1 - i] <= t:
			t += 1
			A[n - 1 - i] = -1
print(ans)
finish = time.time()


from queue import PriorityQueue
__author__ = 'artyom'

def main():
	n = read()
	a = read(3)
	if n == 1:
		return 0
	ix = []
	for i in range(n):
		ix.append(i)
	ix.sort(key=lambda x: a[x])
	l = PriorityQueue()
	r = PriorityQueue()
	i = pos = eg = flag = count = 0
	while eg < n:
		while i < n and a[ix[i]] <= eg:
			if ix[i] > pos:
				r.put(ix[i])
			elif ix[i] < pos:
				l.put(-ix[i])
			else:
				eg += 1
			i += 1
		if flag:
			if l.empty():
				flag = 0
				count += 1
				pos = r.get()
			else:
				pos = -l.get()
		elif r.empty():
			flag = 1
			count += 1
			pos = -l.get()
		else:
			pos = r.get()
		eg += 1
	return count

def read(mode=1, size=None):
	if mode == 0:
		return input().strip()
	if mode == 1:
		return int(input().strip())
	if mode == 2:
		return input().strip().split()
	if mode == 3:
		return list(map(int, input().strip().split()))
	a = []
	for _ in range(size):
		a.append(read(3))
	return a

def write(s='\n'):
	if s is None:
		s = ''
	if isinstance(s, tuple) or isinstance(s, list):
		s = ' '.join(map(str, s))
	s = str(s)
	print(s, end='\n')
write(main())


n = int(input())
a = list(map(int, input().split()))
k = 0
ans = 0
while k != n:
	if ans % 2 == 0:
		for i in range(n):
			if a[i] <= k:
				k += 1
				a[i] = 10000
	else:
		for i in range(n - 1, -1, -1):
			if a[i] <= k:
				k += 1
				a[i] = 10000
	ans += 1
print(ans - 1)


import sys
input = lambda : sys.stdin.readline()
int_arr = lambda : list(map(int, input().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().split())
get_int = lambda : map(int, input().split())
get_flo = lambda : map(float, input().split())
mod = 1000000007

def solve(n, arr):
	c = 0
	tmp = 0
	while set(arr) != {-1}:
		for i in range(n):
			if arr[i] <= tmp and arr[i] != -1:
				arr[i] = -1
				tmp += 1
		arr = arr[::-1]
		c += 1
	print(c - 1)
n = int(input())
arr = int_arr()
solve(n, arr)


n = int(input())
comp = input()
comp = [int(i) for i in comp.split()]
ncollected = [True] * n
direct = 'right'
pieces = 0
pos = 0
changes = 0
while pieces < len(comp):
	for i in range(len(comp)):
		if (comp[i] <= pieces) & (ncollected[i] == True):
			pieces += 1
			ncollected[i] = False
	comp.reverse()
	ncollected.reverse()
	changes += 1
print(changes - 1)


n = int(input())
computers = list(map(int, input().split()))
keys = 0
steps = 0
index = 0
while keys < n:
	if not index > len(computers) - 1:
		if not computers[index] > keys:
			del computers[index]
			keys += 1
		else:
			index += 1
	else:
		computers.reverse()
		index = 0
		steps += 1
print(steps)


n = int(input())
ps = list(map(int, input().split()))
collected = [False for i in range(n)]
infos = 0
direction = 1
i = 0
turns = 0
while infos < n:
	if not collected[i] and ps[i] <= infos:
		collected[i] = True
		infos += 1
		if infos == n:
			break
	if i == (n - 1) * (direction == 1):
		direction *= -1
		turns += 1
	i += 1 * direction
print(turns)


n = int(input())
comp = list(map(int, input().split()))
seen = set()
pieces = 0
direction = 1
res = 0

def begend(dire):
	if dire == 1:
		return (0, n)
	else:
		return (n - 1, -1)
while len(seen) < n:
	for k in range(begend(direction)[0], begend(direction)[1], direction):
		if not k in seen and comp[k] <= pieces:
			seen.add(k)
			pieces += 1
	direction *= -1
	res += 1
print(res - 1)


n = int(input())
a = list(map(int, input().split()))
(x, c) = (0, 0)
while set(a) != {-1}:
	for y in range(n):
		if x >= a[y] and a[y] != -1:
			x += 1
			a[y] = -1
	a = a[::-1]
	c += 1
print(c - 1)


__author__ = 'dell'
n = int(input())
a = list(map(int, input().split()))
reverse = 0
pick = 0
dirshow = 1
i = 0
flagrev = 0
while pick != n:
	if a[i] <= pick:
		a[i] = 9999
		pick += 1
	if pick != n and (i == n - 1 or (i == 0 and flagrev)):
		flagrev = 1
		dirshow *= -1
		reverse += 1
	i += dirshow
print(reverse)


n = int(input())
mas = list(map(int, input().split(' ')))
s = 0
p = 0
res = 0
while s < n:
	if p:
		for i in range(n - 1, -1, -1):
			if mas[i] != -1 and mas[i] <= s:
				s += 1
				mas[i] = -1
		p = 0
	else:
		for i in range(n):
			if mas[i] != -1 and mas[i] <= s:
				s += 1
				mas[i] = -1
		p = 1
	res += 1
print(res - 1)


n = int(input())
l = list(map(int, input().split()))[::-1]
k = 0
p = -1
while k != n:
	l = l[::-1]
	p += 1
	for i in range(n):
		if l[i] != -1 and l[i] <= k:
			k += 1
			l[i] = -1
print(p)


n = int(input())
arr = list(map(int, input().split()))
cnt = 0
turns = 0
used = [False for i in range(n)]
while cnt < n:
	for i in range(n):
		if arr[i] <= cnt and (not used[i]):
			used[i] = True
			cnt += 1
	if cnt == n:
		break
	turns += 1
	for i in range(n - 1, -1, -1):
		if arr[i] <= cnt and (not used[i]):
			used[i] = True
			cnt += 1
	if cnt == n:
		break
	turns += 1
print(turns)


class StdReader:

	def read_int(self):
		return int(self.read_string())

	def read_ints(self, sep=None):
		return [int(i) for i in self.read_strings(sep)]

	def read_float(self):
		return float(self.read_string())

	def read_floats(self, sep=None):
		return [float(i) for i in self.read_strings(sep)]

	def read_string(self):
		return input()

	def read_strings(self, sep=None):
		return self.read_string().split(sep)
reader = StdReader()

def main():
	n = reader.read_int()
	a = reader.read_ints()
	d = 1
	c = 0
	dd = 0
	i = 0
	collected = [False] * n
	while c < n:
		if a[i] <= c and (not collected[i]):
			c += 1
			collected[i] = True
		if c == n:
			break
		if i == n - 1 and d == 1:
			d *= -1
			dd += 1
		if i == 0 and d == -1:
			d *= -1
			dd += 1
		i += d
	print(dd)
main()


n = int(input())
l = list(map(int, input().split()))[::-1]
(total, c) = (0, 0)
while c < n:
	l = l[::-1]
	for (i, e) in enumerate(l):
		if e <= c and e >= 0:
			c += 1
			l[i] = -1
	total += 1
print(total - 1)


from math import *
from collections import deque
from copy import deepcopy
import sys

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def multi():
	return map(int, input().split())

def strmulti():
	return map(str, inp().split())

def lis():
	return list(map(int, inp().split()))

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

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def stringlis():
	return list(map(str, inp().split()))

def out(var):
	sys.stdout.write(str(var))

def printlist(a):
	print(' '.join((str(a[i]) for i in range(len(a)))))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def sum(n):
	sum = 0
	for i in n:
		sum += int(i)
	return sum
n = int(input())
a = lis()
num = 0
ans = 0
while True:
	for i in range(n):
		if a[i] <= num:
			a[i] = n + 1
			num += 1
	if n == num:
		break
	ans += 1
	for i in range(n):
		if a[n - i - 1] <= num:
			a[n - i - 1] = n + 1
			num += 1
	if n == num:
		break
	ans += 1
print(ans)


n = int(input())
comp = list(map(int, input().split(' ')))
done = 0
checked = []
c = 0
t = -1
d = -1
while done < n:
	t += 1
	d *= -1
	if 0 not in checked:
		if comp[0] == 0:
			done += 1
			checked.append(0)
	c += d
	while c >= 0 and c < n:
		if c not in checked:
			if comp[c] <= done:
				done += 1
				checked.append(c)
		c += d
print(t)


n = int(input())
a = [int(x) for x in input().split()]
b = [True] * n
inst = 0
d = 0
while inst < n:
	if d % 2 == 0:
		for i in range(0, n):
			if b[i] and a[i] <= inst:
				inst += 1
				b[i] = False
	else:
		for i in range(n - 1, -1, -1):
			if b[i] and a[i] <= inst:
				inst += 1
				b[i] = False
	d += 1
print(d - 1)


n = int(input())
b = list(map(int, input().split()))
e = ans = 0
while True:
	temp = []
	ans += 1
	for i in range(len(b)):
		if e >= b[i]:
			e += 1
		else:
			temp.append(b[i])
	temp.reverse()
	b = temp
	if len(temp) == 0:
		break
print(ans - 1)


n = int(input())
x = list(map(int, input().split()))
c = 0
t = -1
while True:
	t += 1
	for i in range(n):
		if x[i] != 'X' and x[i] <= c:
			c += 1
			x[i] = 'X'
	if c == n:
		break
	t += 1
	for i in range(n - 1, -1, -1):
		if x[i] != 'X' and x[i] <= c:
			c += 1
			x[i] = 'X'
	if c == n:
		break
print(t)


def read_data():
	n = int(input())
	As = list(map(int, input().split()))
	return (n, As)

def solve(n, As):
	s = 0
	turn = 0
	direction = 0
	poss = [list(range(n)), list(range(n - 1, -1, -1))]
	while s < n:
		for pos in poss[direction]:
			a = As[pos]
			if a <= s:
				s += 1
				As[pos] = n + 1
				if s == n:
					return turn
		direction = 1 - direction
		turn += 1
(n, As) = read_data()
print(solve(n, As))


n = int(input())
arr = list(map(int, input().split()))
info = 0
num = 0
count = -1
while num != n:
	count += 1
	if count % 2 == 0:
		for i in range(n):
			if arr[i] <= info:
				arr[i] = 2000
				info += 1
				num += 1
	else:
		for i in range(-1, -n - 1, -1):
			if arr[i] <= info:
				arr[i] = 2000
				info += 1
				num += 1
print(count)


import math
from collections import defaultdict

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

def solve():
	n = int(input())
	a = input_ints()
	use = [0] * n
	x = 0
	for i in range(n):
		for j in range(n) if i % 2 == 0 else range(n - 1, -1, -1):
			if not use[j] and x >= a[j]:
				x += 1
				use[j] = 1
		if x == n:
			print(i)
			return
solve()


n = int(input())
a = list(map(int, input().split()))
info = 0
i = 0
di = 0
while 1:
	for i in range(n):
		if a[i] == -69:
			continue
		if a[i] <= info:
			info += 1
			a[i] = -69
	if info == n:
		break
	a.reverse()
	di += 1
print(di)


n = int(input())
lists = [int(i) for i in input().split()]
di = 1
num = 0
loc = 1
total = 0
if lists[0] == 0:
	num += 1
	lists[0] = n + 1
while num < n:
	num1 = num
	if loc == n and di == 1 or (loc == 1 and di == -1):
		di *= -1
		total += 1
	if di == 1:
		for i in range(loc, n):
			if num >= lists[i]:
				loc = i + 1
				lists[i] = n + 1
				num += 1
				break
	else:
		for i in range(n + 2 - loc, n + 1):
			if num >= lists[-i]:
				loc = n - i + 1
				lists[-i] = n + 1
				num += 1
				break
	if num1 == num:
		di *= -1
		total += 1
print(total)


n = int(input())
a = list(map(int, input().split()))
z = [0 for i in range(n)]
c = 0
ans = 0
f = 0
while set(z) != {1}:
	if f == 0:
		for j in range(len(a)):
			if a[j] <= c and z[j] != 1:
				c += 1
				z[j] = 1
		f = 1
	else:
		for j in range(len(a) - 1, -1, -1):
			if a[j] <= c and z[j] != 1:
				c += 1
				z[j] = 1
		f = 0
	ans += 1
print(ans - 1)


n = int(input())
a_cost = [int(x) for x in input().split()]
visit = [0 for i in range(n)]
nbPieces = 0
pos = 0
cur_dir = 1
nbChangements = 0
while True:
	if nbPieces >= a_cost[pos] and visit[pos] == 0:
		nbPieces += 1
		visit[pos] += 1
	if nbPieces == n:
		print(nbChangements)
		break
	pos += cur_dir
	if pos == n:
		cur_dir = -1
		pos += cur_dir
		nbChangements += 1
	if pos == -1:
		cur_dir = 1
		pos += cur_dir
		nbChangements += 1


import time
(COL, ROW, LEN) = (1000, 1000, 26)
matrix = [[0] * COL for i in range(ROW)]
arr = [0] * LEN
direction = ((0, -1), (-1, 0), (0, 1), (1, 0))

class Pair:

	def __init__(self, x, y):
		self.x = x
		self.y = y

def timer(func, *pargs, **kargs):
	start = time.time()
	func(*pargs, **kargs)
	return time.time() - start

def test(a):
	answer = 0
	for value in a:
		if value >= 0:
			answer += value
	return answer
n = int(input())
arr = list(map(int, input().split()))
ans = -1
volume = 0
direct = 0
if test(arr) == 0:
	ans += 1
while test(arr):
	ans += 1
	if direct == 0:
		for (ind, val) in enumerate(arr):
			if val <= volume and val != -1:
				arr[ind] = -1
				volume += 1
	else:
		for ind in range(len(arr) - 1, -1, -1):
			if arr[ind] <= volume and arr[ind] != -1:
				arr[ind] = -1
				volume += 1
	direct += 1
	direct %= 2
print(ans)


import sys
num_computers = int(input())
if num_computers < 1 or num_computers > 1000:
	sys.exit()
input_inf = input().split()
computers = [int(i) for i in input_inf]
new_computers = []
num_information = 0
num_turns = 0
current_computer = 0
while num_computers != num_information:
	for i in computers:
		if num_information >= i:
			num_information += 1
		else:
			new_computers.append(i)
	if len(new_computers) != 0:
		computers.clear()
		new_computers.reverse()
		computers.extend(new_computers)
		new_computers.clear()
		num_turns += 1
print(num_turns)


n = int(input())
list = [int(x) for x in input().split()]
count = 0
x = 0
i = 0
while list != []:
	count += 1
	i = 0
	while i < len(list):
		if list[i] <= x:
			del list[i]
			x += 1
		else:
			i += 1
	if list != []:
		count += 1
		i = len(list) - 1
		while i >= 0:
			if list[i] <= x:
				x += 1
				del list[i]
			i -= 1
count -= 1
print(count)


n = int(input())
security = [int(i) for i in input().split()]
haveset = set()
info = 0
x = 0
dx = 1
turns = 0
while True:
	if x not in haveset and info >= security[x]:
		haveset.add(x)
		info += 1
		if info == n:
			break
	x += dx
	if x == len(security):
		x -= 2
		dx = -1
		turns += 1
	elif x == -1:
		x += 2
		dx = 1
		turns += 1
print(turns)


from sys import *
input = lambda : stdin.readline()
int_arr = lambda : list(map(int, stdin.readline().strip().split()))
str_arr = lambda : list(map(str, stdin.readline().split()))
get_str = lambda : map(str, stdin.readline().strip().split())
get_int = lambda : map(int, stdin.readline().strip().split())
get_float = lambda : map(float, stdin.readline().strip().split())
mod = 1000000007
setrecursionlimit(1000)
n = int(input())
arr = int_arr()
ct = 0
cond = 0
tot = -1
while True:
	if len(set(arr)) == 1 and arr[0] == -1:
		break
	if cond % 2 == 0:
		cond = 1
		tot += 1
		for i in range(n):
			if arr[i] != -1 and arr[i] <= ct:
				ct += 1
				arr[i] = -1
				last = i
	else:
		cond = 2
		tot += 1
		for i in range(last, -1, -1):
			if arr[i] != -1 and arr[i] <= ct:
				ct += 1
				arr[i] = -1
print(tot)


3
import itertools as ittls
from collections import Counter
from pprint import pprint as pprint
import re
import math

def sqr(x):
	return x * x

def inputarray(func=int):
	return list(map(func, input().split()))
N = int(input())
A = list(inputarray())
(power, i) = (0, 0)
mask = [0] * N
while power < N:
	s = 0
	domain = list(range(N)) if i & 1 != 1 else list(range(N - 1, -1, -1))
	for j in domain:
		if mask[j] == 0 and A[j] <= power:
			mask[j] = 1
			power = power + 1
	i = i + 1
print(i - 1)


n = int(input())
an = list(map(int, input().split()))
arr = []
allcnt = 0
for i in range(0, n):
	if allcnt >= an[i]:
		allcnt += 1
	else:
		arr.append(an[i])
an = list(arr)
res = 0
while len(an) > 0:
	res += 1
	arrt = []
	for i in range(len(an) - 1, -1, -1):
		if allcnt >= an[i]:
			allcnt += 1
		else:
			arrt.append(an[i])
	an = list(arrt)
print(res)


import sys
lines = iter(sys.stdin.read().split('\n'))
n = int(next(lines))
nums = list(map(int, next(lines).strip(' ').split(' ')))
total = 0
count = 0
turns = -1
direction = 1

def traverse(nums, direction, total, count):
	if direction == 0:
		for (i, num) in enumerate(nums):
			if num != -1 and num <= total:
				count += 1
				total += 1
				nums[i] = -1
	elif direction == 1:
		for i in range(len(nums) - 1, -1, -1):
			num = nums[i]
			if num != -1 and num <= total:
				count += 1
				total += 1
				nums[i] = -1
	return (total, count)
while count < n:
	turns += 1
	direction = 1 - direction
	(total, count) = traverse(nums, direction, total, count)
print(turns)


n = int(input())
lst = list(map(int, input().split()))
ans = 0
zap = 0
used = [False] * n
while zap != n:
	if zap != 0:
		ans += 1
	for i in range(n):
		if not used[i] and zap >= lst[i]:
			used[i] = True
			zap += 1
	if zap == n:
		break
	ans += 1
	for i in range(n - 1, -1, -1):
		if not used[i] and zap >= lst[i]:
			used[i] = True
			zap += 1
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
k = 0
pov = 0
used = [False] * n
for i in range(n):
	for j in range(n):
		if used[j] == False and a[j] <= k:
			used[j] = True
			k += 1
	r = 0
	for j in range(n):
		if used[j] == False:
			r = 1
	if r == 0:
		print(pov)
		break
	else:
		pov += 1
	for j in range(n - 1, -1, -1):
		if used[j] == False and a[j] <= k:
			used[j] = True
			k += 1
	r = 0
	for j in range(n):
		if used[j] == False:
			r = 1
	if r == 0:
		print(pov)
		break
	else:
		pov += 1


import sys
n = int(input())
a = list(map(int, sys.stdin.readline().split()))
nbTurn = -1
nbParts = 0

def check(a):
	a = [i for i in a if i > 0]
while a:
	nbTurn += 1
	for i in range(len(a)):
		if a[i] <= nbParts:
			a[i] = -1
			nbParts += 1
	a = [i for i in a if i > 0]
	if len(a) == 0:
		break
	nbTurn += 1
	for i in range(len(a) - 1, -1, -1):
		if a[i] <= nbParts:
			a[i] = -1
			nbParts += 1
	a = [i for i in a if i > 0]
print(nbTurn)


from math import inf
n = int(input().strip())
arr = list(map(int, input().strip().split()))
points = 0
dr = 1
changes = 0
while True:
	if dr == 1:
		for i in range(n):
			if arr[i] <= points:
				points += 1
				arr[i] = inf
		dr = -1
	else:
		for i in range(n - 1, -1, -1):
			if arr[i] <= points:
				points += 1
				arr[i] = inf
		dr = 1
	if any((i != inf for i in arr)):
		changes += 1
	else:
		exit(print(changes))


n = int(input())
a = [int(i) for i in input().split()]
t = 0
ii = 0
h = 0
while t != n:
	for i in range(n):
		if a[i] <= ii and a[i] != -1:
			t += 1
			ii += 1
			a[i] = -1
	if t != n:
		h += 1
		for i in range(n - 1, -1, -1):
			if a[i] != -1:
				if ii >= a[i]:
					t += 1
					ii += 1
					a[i] = -1
	if t != n:
		h += 1
print(h)


n = int(input())
a = list(map(int, input().split()))
sum = 0
ans = 0
f = True
while sum != n:
	for i in range(n):
		if a[i] <= sum:
			a[i] = 100000000000000000000000
			sum += 1
			if sum == n:
				f = False
				break
	if not f:
		break
	ans += 1
	for i in range(n - 1, -1, -1):
		if a[i] <= sum:
			a[i] = 1000000000000000000000000000
			sum += 1
			if sum == n:
				f = False
				break
	if not f:
		break
	ans += 1
print(ans)


n = int(input())
arr = [int(x) for x in input().split()]
c = 0
ans = 0
cnt = 0
for i in range(n):
	for j in range(n):
		if arr[j] == -1:
			cnt += 1
		elif arr[j] <= c:
			c += 1
			arr[j] = -1
			cnt += 1
	if cnt == n:
		break
	else:
		cnt = 0
	ans += 1
	for j in range(n - 1, -1, -1):
		if arr[j] == -1:
			cnt += 1
		elif arr[j] <= c:
			c += 1
			arr[j] = -1
			cnt += 1
	if cnt == n:
		break
	else:
		cnt = 0
	ans += 1
print(ans)


trash = input()
line1 = [int(x) for x in input().split()]
cred = 0
switches = -1
while max(line1) != -1:
	switches += 1
	for i in range(0, len(line1), 1):
		if line1[i] <= cred and line1[i] != -1:
			cred += 1
			line1[i] = -1
	if max(line1) != -1:
		switches += 1
		for i in range(len(line1) - 1, -1, -1):
			if line1[i] <= cred and line1[i] != -1:
				cred += 1
				line1[i] = -1
print(switches)


def main():
	n = int(input())
	arr = [int(x) for x in input().split()]
	used = [False] * n
	napr = True
	c = 0
	ans = 0
	while False in used:
		if napr:
			for i in range(n):
				if c >= arr[i] and used[i] == False:
					used[i] = True
					c += 1
		else:
			for i in range(n - 1, 0 - 1, -1):
				if c >= arr[i] and used[i] == False:
					used[i] = True
					c += 1
		napr = not napr
		if False in used:
			ans += 1
	print(ans)
main()


def mp():
	return map(int, input().split())

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

def pt(x):
	print(x)

def ip():
	return input()

def it():
	return int(input())

def sl(x):
	return [t for t in x]

def spl(x):
	return x.split()

def aj(liste, item):
	liste.append(item)

def bin(x):
	return '{0:b}'.format(x)

def printlist(l):
	print(' '.join([str(x) for x in l]))

def listring(l):
	return ''.join([str(x) for x in l])
n = it()
a = lt()
i = 0
visited = [False for i in range(n)]
sum = 0
sens = 1
piece = 0
nott = 0
c = 0
while sum < n:
	if sens == 1:
		if i <= n - 1:
			if piece >= a[i] and (not visited[i]):
				piece += 1
				visited[i] = True
				sum += 1
			i += 1
		else:
			sens = -1
			i = n - 1
			c += 1
	elif sens == -1:
		if i >= 0:
			if piece >= a[i] and (not visited[i]):
				piece += 1
				visited[i] = True
				sum += 1
			i -= 1
		else:
			sens = 1
			i = 0
			c += 1
print(c)


n = int(input())
visited = [0] * n
arr = list(map(int, input().split()))
s = 0
cnt = 0
while s < n:
	for i in range(0, n):
		if s >= arr[i] and visited[i] == 0:
			s += 1
			visited[i] = 1
	if s != n:
		cnt += 1
	i = n - 1
	while i >= 0:
		if s >= arr[i] and visited[i] == 0:
			s += 1
			visited[i] = 1
		i -= 1
	if s != n:
		cnt += 1
print(cnt)


n = int(input())
l_n = list(map(int, input().split()))
s_s = set()
r = 1
c = 0
i = 0
t = -1
while len(s_s) != n:
	if c not in s_s and i >= l_n[c]:
		i += 1
		s_s.add(c)
		if len(s_s) == n:
			break
	if c == 0:
		r = 1
		t += 1
	if c == n - 1:
		r = -1
		t += 1
	c += r
if n == 1:
	print(0)
else:
	print(t)


from itertools import combinations
__author__ = 'ploskov'

def main():
	n = int(input())
	computers = list(map(int, input().split()))
	current_info = 0
	reverses = 0
	while computers:
		i = 0
		while i < len(computers):
			if computers[i] <= current_info:
				computers.pop(i)
				current_info += 1
			else:
				i += 1
		computers.reverse()
		reverses += 1
	print(reverses - 1)
main()


import copy

def solveLeft(elems, acum=0):
	changed = False
	for i in range(len(elems)):
		if elems[i] is not None and elems[i] <= acum:
			acum += 1
			elems[i] = None
			changed = True
	if not changed:
		return 0
	return 1 + solveRight(elems, acum)

def solveRight(elems, acum=0):
	changed = False
	for i in reversed(range(len(elems))):
		if elems[i] is not None and elems[i] <= acum:
			acum += 1
			elems[i] = None
			changed = True
	if not changed:
		return 0
	return 1 + solveLeft(elems, acum)

def solve(elems):
	return solveLeft(copy.copy(elems)) - 1
n = int(input())
elems = list(map(int, input().split()))
print(solve(elems))


n = int(input())
coms = [int(x) for x in input().split()]
res = 0
state = []
collected = 0
for i in range(n):
	state.append(1)
i = 0
turnRight = True
while collected < n:
	if state[i] == 1:
		if coms[i] <= collected:
			state[i] = 0
			collected += 1
	if collected == n:
		print(res)
		exit()
	if turnRight:
		i += 1
		if i == n:
			res += 1
			turnRight = False
			i = n - 1
	else:
		i -= 1
		if i == -1:
			res += 1
			turnRight = True
			i = 0


n = int(input())
l = list(map(int, input().split()))
f = [0] * n
k = 0
ans = 0
while k < n:
	for i in range(n):
		if f[i] == 0 and l[i] <= k:
			k += 1
			f[i] = 1
	if k == n:
		break
	ans += 1
	for i in range(n):
		if f[n - i - 1] == 0 and l[n - i - 1] <= k:
			k += 1
			f[n - i - 1] = 1
	if k == n:
		break
	ans += 1
print(ans)


values = int(input())
array = list(map(int, input().split()))
counter = 0
ways = 0
while counter != values:
	for i in range(0, values):
		if array[i] != -1 and array[i] <= counter:
			counter += 1
			array[i] = -1
	if counter == values:
		break
	ways += 1
	for i in range(values - 1, -1, -1):
		if array[i] != -1 and array[i] <= counter:
			counter += 1
			array[i] = -1
	if counter == values:
		break
	ways += 1
print(ways)


def getNum(n, arr):
	st1 = []
	st2 = []
	worth = 0
	count = 0
	for i in range(n):
		if arr[i] <= worth:
			worth += 1
		else:
			st1.append(arr[i])
	if len(st1) == 0:
		return count
	count += 1
	while len(st1) != 0:
		if st1[-1] <= worth:
			st1.pop()
			worth += 1
		else:
			st2.append(st1.pop())
		if len(st1) == 0:
			if len(st2) == 0:
				return count
			st1 = st2
			st2 = []
			count += 1
	return count
n = int(input())
arr = list(map(int, input().split()))
print(getNum(n, arr))


n = int(input())
lst = list(map(int, input().strip().split(' ')))
if n == 1:
	print(0)
else:
	i = 0
	c = 0
	pc = 0
	f = 1
	while len(lst) != 0:
		if lst[i] <= pc:
			del lst[i]
			if len(lst) == 0:
				break
			elif f == 1:
				if i >= len(lst):
					c += 1
					i -= 1
					f = 0
			elif i != 0:
				i -= 1
			elif i == 0:
				c += 1
				f = 1
			pc += 1
		elif f == 1:
			if i + 1 < len(lst):
				i += 1
			else:
				c += 1
				f = 0
				if i - 1 >= 0:
					i -= 1
				else:
					break
		elif f == 0:
			if i - 1 >= 0 and len(lst) != 0:
				i -= 1
			else:
				c += 1
				f = 1
				if i + 1 < len(lst):
					i += 1
				else:
					break
	print(c)


n = int(input())
l = list(map(int, input().rstrip().split(' ')))
s = sum(l)
i = 0
t = 0
turns = 0
g_u = True
while s != -1 * n:
	if l[i] <= t and l[i] > -1:
		t += 1
		s -= l[i] + 1
		l[i] = -1
	if s == -1 * n:
		break
	if g_u:
		i = i + 1
		if i >= n:
			g_u = False
			i = i - 2
			turns += 1
	else:
		i = i - 1
		if i < 0:
			g_u = True
			i = i + 2
			turns += 1
print(turns)


from sys import stdin
live = True
if not live:
	stdin = open('data.in', 'r')
n = int(stdin.readline().strip())
c = list(map(int, stdin.readline().strip().split()))
g = dir = 0
ans = -1
while g != n:
	ans += 1
	if dir == 0:
		for it in range(n):
			if c[it] <= g and c[it] != -1:
				g += 1
				c[it] = -1
	else:
		for it in range(n - 1, -1, -1):
			if c[it] <= g and c[it] != -1:
				g += 1
				c[it] = -1
	dir = 1 - dir
print(ans)
if not live:
	stdin.close()


n = int(input())
a = list(map(int, input().split()))
count = 0
ans = 0
while count < n:
	tmp = []
	m = len(a)
	for i in range(m):
		if count >= a[i]:
			count += 1
		else:
			tmp.append(a[i])
	if count < n:
		a = tmp[::-1]
		ans += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
s = 0
r = 0
i = 0
k = 1
f = 0
while s != n:
	if a[i] <= s:
		s += 1
		a[i] = 9999
	if s != n and (i == n - 1 or (i == 0 and f)):
		r += 1
		k *= -1
		f = 1
	i += k
print(r)


n = int(input())
lst = [int(x) for x in input().split()]
power = 0
answer = 0
while power != len(lst):
	for i in range(len(lst)):
		if lst[i] <= power:
			power += 1
			lst[i] = float('inf')
	if power == len(lst):
		break
	answer += 1
	for i in range(len(lst) - 1, -1, -1):
		if lst[i] <= power:
			power += 1
			lst[i] = float('inf')
	if power == len(lst):
		break
	answer += 1
print(answer)


n = int(input())
A = [int(i) for i in input().split()]
visited = [0 for i in range(n)]
now = 0
result = 0

def left():
	global n, result, now
	result += 1
	for i in range(n):
		if visited[i] == 0 and A[i] <= now:
			visited[i] = 1
			now += 1

def right():
	global n, result, now
	result += 1
	for i in range(n - 1, -1, -1):
		if visited[i] == 0 and A[i] <= now:
			visited[i] = 1
			now += 1
while now != n:
	left()
	if now != n:
		right()
print(result - 1)


x = int(input())
ls = input().split()
for i in range(len(ls)):
	ls[i] = int(ls[i])
ans = 0
chk = [False] * x
cnt = 0
index = 0
direction = True
while cnt < x:
	if not chk[index] and cnt >= ls[index]:
		chk[index] = True
		cnt += 1
	if cnt == x:
		break
	index += 1 if direction else -1
	if index == x:
		direction = False
		index = x - 2
		ans += 1
	if index == -1:
		direction = True
		index = 1
		ans += 1
print(ans)


N = int(input())
A = list(map(int, input().split()))
L = [False for i in range(N)]
i = 0
cnt = 0
ans = 0
flg = True
while True:
	if not L[i] and A[i] <= cnt:
		L[i] = True
		cnt += 1
	if cnt == N:
		break
	if flg:
		i += 1
		if i == N:
			flg = False
			i = N - 1
			ans += 1
	else:
		i -= 1
		if i == -1:
			flg = True
			i = 0
			ans += 1
print(ans)


n = int(input())
c = list(map(int, input().split()))
l = 0
s = 0
p = 1
i = 0
while s != n:
	if c[i] != -1 and s >= c[i]:
		s += 1
		c[i] = -1
	i += p
	if (i == n or i == -1) and s != n:
		p *= -1
		l += 1
		i += 2 * p
print(l)


n = int(input())
line = list(map(int, input().split()))
(num, res, flag) = (0, 0, True)
while num < n:
	res += 1
	if flag:
		flag = False
		for i in range(n):
			if 0 <= line[i] <= num:
				num += 1
				line[i] = -1
	else:
		flag = True
		for i in range(n - 1, -1, -1):
			if 0 <= line[i] <= num:
				num += 1
				line[i] = -1
print(res - 1)


n = int(input())
a = list(map(int, input().split()))
(turn, cnt, ans) = (0, 0, -1)
li = [0] * n
while 0 in li:
	ans += 1
	if turn == 0:
		for i in range(n):
			if a[i] <= cnt and li[i] == 0:
				cnt += 1
				li[i] = 1
		if 0 in li:
			turn = 1
	else:
		for i in range(n - 1, -1, -1):
			if a[i] <= cnt and li[i] == 0:
				cnt += 1
				li[i] = 1
		if 0 in li:
			turn = 0
print(ans)


n = int(input())
a = list(map(int, input().split()))
x = 0
r = 0
i = 0
while 1:
	while i < len(a):
		if a[i] <= x:
			x += 1
			a = a[:i] + a[i + 1:]
		else:
			i += 1
	if not a:
		break
	r += 1
	while i > 0:
		i -= 1
		if a[i] <= x:
			x += 1
			a = a[:i] + a[i + 1:]
	if not a:
		break
	r += 1
print(r)


n = int(input())
a = list(map(int, input().split()))
used = [False for i in range(n)]
k = 0
for i in range(n):
	flag = False
	for j in range(n):
		if a[j] <= k and used[j] == False:
			used[j] = True
			k += 1
	for j in range(n):
		if used[j] == False:
			flag = True
	if flag == False:
		print(2 * i)
		break
	for j in range(n - 1, -1, -1):
		if a[j] <= k and used[j] == False:
			used[j] = True
			k += 1
	flag = False
	for j in range(n):
		if used[j] == False:
			flag = True
	if flag == False:
		print(2 * i + 1)
		break


n = int(input())
a = list(map(int, input().split()))
done = n * [False]
passes = 0
clues = 0
pos = 0
dir = 1
while clues != n:
	while pos >= 0 and pos < n:
		if not done[pos] and a[pos] <= clues:
			clues += 1
			done[pos] = True
		pos += dir
	passes += 1
	dir *= -1
	pos += dir
print(passes - 1)


from sys import stdin

def get_ints():
	return list(map(int, stdin.readline().strip().split()))
n = int(input())
ar = get_ints()
c = 0
i = 0
turns = 0
dir = 1
first = True
done = [False] * n
while c < n:
	if ar[i] <= c and (not done[i]):
		done[i] = True
		c += 1
	if c < n:
		if i == 0 or i == n - 1:
			if first:
				first = False
			else:
				dir = -dir
				turns += 1
		i += dir
print(turns)


n = int(input())
l = list(map(int, input().split()))
m = [0 for i in range(n)]
num_switch = 0
num_unlock = 0
direction = 1
i = 0
while num_unlock < n:
	if i > n - 1:
		i = n - 1
		num_switch += 1
		direction = -1
	if i < 0:
		i = 0
		num_switch += 1
		direction = 1
	if l[i] <= num_unlock and l[i] != -1:
		num_unlock += 1
		l[i] = -1
	i += direction
print(num_switch)


a = int(input())
g = [int(i) for i in input().split()]
f = []
for j in range(a):
	f.append(False)
s = 0
k = 0
q = 0
p = 1
i = 0
while s < a:
	i = i + 1
	if f[k] == False:
		if g[k] <= s:
			f[k] = True
			s = s + 1
	if s < a:
		if k == a - 1:
			q = q + 1
			p = -1
		elif k == 0 and i != 1:
			q = q + 1
			p = 1
	k = k + p
print(q)


n = int(input())
a = [int(i) for i in input().split()]
myinfo = 0
res = 0
while a:
	i = 0
	while i < len(a):
		if myinfo >= a[i]:
			myinfo += 1
			del a[i]
			i -= 1
		i += 1
	a = a[::-1]
	res += 1
print(res - 1)


n = int(input())
C = list(map(int, input().split()))

def check(init):
	d = init
	done = [False for i in range(n)]
	cracked = 0
	changed = 0
	while cracked < n:
		if d == 0:
			for i in range(n):
				if not done[i] and C[i] <= cracked:
					done[i] = True
					cracked += 1
		else:
			for i in range(n)[::-1]:
				if not done[i] and C[i] <= cracked:
					done[i] = True
					cracked += 1
		d = d ^ 1
		if cracked < n:
			changed += 1
	return changed
print(check(0))


n = int(input())
a = list(map(int, input().split()))
if n == 1:
	print(0)
else:
	total = 0
	turns = -1
	idx = 0
	d = 'r'
	while total <= n - 1:
		if a[idx] != -1 and a[idx] <= total:
			total += 1
			a[idx] = -1
		if total == n:
			break
		if idx == len(a) - 1:
			d = 'l'
			turns += 1
		elif idx == 0:
			d = 'r'
			turns += 1
		if d == 'l':
			idx -= 1
		elif d == 'r':
			idx += 1
	print(turns)


n = int(input())
a = list(map(int, input().split()))
turn = -1
current = 0
while current < n:
	turn += 1
	for i in range(n):
		if a[i] >= 0 and a[i] <= current:
			a[i] = -1
			current += 1
	a.reverse()
print(turn)


def main():
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	b = [False for i in a]
	if n == 1:
		print(0)
		return
	turns = 0
	parts = 0
	incr = 1
	i = 0
	while parts < n:
		while a[i] > parts:
			i += incr
			if i == n or i < 0:
				turns += 1
				incr *= -1
				i += 2 * incr
		if b[i]:
			i += incr
			if i == n or i < 0:
				turns += 1
				incr *= -1
				i += 2 * incr
			continue
		parts += 1
		if parts == n:
			break
		b[i] = True
		i += incr
		if i == n or i < 0:
			turns += 1
			incr *= -1
			i += 2 * incr
	print(turns)
main()


n = int(input())
v = list(map(int, input().split()))
cnt = 0
rot = 0
use = {}
while cnt < n:
	for i in range(n):
		if i not in use and cnt >= v[i]:
			cnt += 1
			use[i] = 1
	if cnt != n:
		rot += 1
	for i in reversed(range(n)):
		if i not in use and cnt >= v[i]:
			cnt += 1
			use[i] = 1
	if cnt != n:
		rot += 1
print(rot)


n = int(input())
A = list(map(int, input().split()))
curr = 0
rot = 0
while curr < n:
	for i in range(0, n):
		if A[i] <= curr:
			curr += 1
			A[i] = n + 3
	if curr != n:
		rot += 1
	for i in range(n - 1, -1, -1):
		if A[i] <= curr:
			curr += 1
			A[i] = n + 3
	if curr != n:
		rot += 1
print(rot)


n = int(input())
s = list(map(int, input().split()))
c = [0] * n
i = 0
o = 1
p = -1
d = 0
while i != n:
	if p + o < 0 or p + o >= n:
		o *= -1
		d += 1
	p += o
	if c[p] == 0 and s[p] <= i:
		c[p] = 1
		i += 1
print(d)


n = int(input())
a = list(map(int, input().split()))
direct = 1
pos = 0
now = 0
res = 0
checked = [0] * n
count = 0
while now < n:
	if count < 10:
		count += 1
	if direct == 1:
		good = 0
		for i in range(pos, n):
			if checked[i]:
				continue
			if a[i] <= now:
				checked[i] = 1
				now += 1
				pos = i
				good = 1
				break
		if not good:
			res += 1
			direct = 0
	if now == n:
		break
	if count < 10:
		count += 1
	if direct == 0:
		good = 0
		for i in range(pos - 1, -1, -1):
			if checked[i]:
				continue
			if a[i] <= now:
				checked[i] = 1
				now += 1
				pos = i
				good = 1
				break
		if not good:
			direct = 1
			res += 1
print(res)


n = int(input(''))
s = input('')
a = s.split(' ')
arr = []
for i in range(0, n):
	arr.append(int(a[i]))
s = -1
num = 0
d = {}
k = 1
while num < n:
	for i in range(0, n):
		l = i
		if k == -1:
			l = n - i - 1
		try:
			x = d[l]
		except:
			if arr[i] <= num:
				num += 1
				d[l] = 1
	arr = arr[::-1]
	s += 1
	k *= -1
print(s)


n = int(input())
A = [int(a) for a in input().split()]
changes = 0
direction = 1
x = n
inf = 0
location = -1
while x > 0:
	if direction == 1:
		for i in range(location + 1, n):
			if A[i] <= inf:
				A[i] = n
				inf += 1
				x -= 1
				location = i
		direction = 0
	elif direction == 0:
		for i in range(location - 1, -1, -1):
			if A[i] <= inf:
				A[i] = n
				inf += 1
				x -= 1
				location = i
			direction = 1
	changes += 1
print(changes - 1)


n = int(input())
a = list(map(int, input().split()))
visited = [False] * n
c = 0
cycles = 0
order = 1
while not all(visited):
	for (idx, i) in enumerate(a[::order]):
		if order == -1:
			idx = n - idx - 1
		if not visited[idx] and i <= c:
			c += 1
			visited[idx] = True
	if all(visited):
		break
	order = 1 if order < 0 else -1
	cycles += 1
print(cycles)


import sys
n = int(input())
lst = [int(i) for i in input().split()]
was = [False for i in range(n)]
cur = 0
ind = 0
ans = 0
curdir = 1
while cur < n:
	if not was[ind]:
		if lst[ind] <= cur:
			cur += 1
			was[ind] = True
			if cur == n:
				break
	if ind == n - 1 and curdir > 0 or (ind == 0 and curdir < 0):
		curdir = -curdir
		ans += 1
	ind += curdir
print(ans)


from sys import stdin
from collections import defaultdict
n = int(stdin.readline())
arr = list(map(int, stdin.readline().rstrip().split(' ')))
d = defaultdict(lambda : False)
turns = 0
taken = 0
i = 0
while taken != n:
	if turns % 2 == 0:
		i = 0
		while i < n:
			if arr[i] <= taken and (not d[i]):
				taken += 1
				d[i] = True
			i += 1
	else:
		i = n - 1
		while i >= 0:
			if arr[i] <= taken and (not d[i]):
				taken += 1
				d[i] = True
			i -= 1
	turns += 1
print(turns - 1)


(n, A) = (int(input()), list(enumerate(map(int, input().split()))))
(B, pieces, turns) = (A[::-1], set(), 0)
while True:
	for (i, a) in A:
		if a <= len(pieces):
			pieces.add(i)
	if len(pieces) == n:
		break
	turns += 1
	for (i, a) in B:
		if a <= len(pieces):
			pieces.add(i)
	if len(pieces) == n:
		break
	turns += 1
print(turns)


n = int(input())
A = list(map(int, input().split()))
(s, ret) = (0, 0)
while True:
	for i in range(n):
		if A[i] <= s:
			A[i] = n + 1
			s += 1
	if s == n:
		break
	ret += 1
	for i in range(n - 1, -1, -1):
		if A[i] <= s:
			A[i] = n + 1
			s += 1
	if s == n:
		break
	ret += 1
print(ret)


n = int(input())
arr = list(map(int, input().split()))
(it, res, mx, i, j) = (0, 0, -1, 1, n - 1)
vis = [0] * n
while it < n:
	if j == mx:
		i = -i
		j = abs(j - n + 1)
		res += 1
	mx += i
	if it >= arr[mx] and vis[mx] == 0:
		vis[mx] = 1
		it += 1
print(res)


n = int(input())
crypt = list(map(int, input().split()))
i = 0
pocet_inf = 0
rotates = 0
direction = 'forvard'
count_inf = sum(crypt)
geted_inf = 0
first_iter = True
while True:
	if crypt[i] != 'checked' and crypt[i] <= pocet_inf:
		pocet_inf += 1
		geted_inf += crypt[i]
		crypt[i] = 'checked'
	if geted_inf == count_inf:
		break
	else:
		if i == len(crypt) - 1:
			direction = 'back'
			rotates += 1
		if i == 0 and (not first_iter):
			direction = 'forvard'
			rotates += 1
	if direction == 'forvard':
		i += 1
	elif direction == 'back':
		i -= 1
	first_iter = False
print(rotates)


n = int(input())
a = list(map(int, input().split()))
cnt = 0
d = 0
while len(a) > 0:
	b = []
	for i in range(len(a)):
		if a[i] <= d:
			d += 1
		else:
			b.append(a[i])
	a = b[::-1]
	cnt += 1
print(cnt - 1)


import os
import sys
from io import BytesIO, IOBase
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')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans
n = int(input())
nums = list(map(int, input().split()))
check = [1] * n
(pieces, cnt, ans, last) = (0, 0, 1, 0)
while check.count(0) != n:
	for i in range(n):
		if pieces >= nums[i] and check[i] == 1:
			check[i] = 0
			pieces += 1
			if i > last:
				if ans == 0:
					cnt += 1
					ans ^= 1
			elif i < last:
				if ans == 1:
					cnt += 1
					ans ^= 1
			last = i
	for i in range(n - 1, -1, -1):
		if pieces >= nums[i] and check[i] == 1:
			check[i] = 0
			pieces += 1
			if i > last:
				if ans == 0:
					cnt += 1
					ans ^= 1
			elif i < last:
				if ans == 1:
					cnt += 1
					ans ^= 1
			last = i
print(cnt)


n = int(input())
required = input().split()
hacked = []
for i in range(0, n):
	required[i] = int(required[i])
	hacked.append(False)
turns = 0
got = 0
r = range(0, n)
l = range(n - 1, -1, -1)
right = True
while got != n:
	if right:
		for i in r:
			if required[i] <= got and (not hacked[i]):
				got += 1
				hacked[i] = True
	else:
		for i in l:
			if required[i] <= got and (not hacked[i]):
				got += 1
				hacked[i] = True
	turns += 1
	right = not right
print(turns - 1)


n = int(input())
lis = list(map(int, input().split()))
c = 0
ans = prev = 0
ans = -1
while c < n:
	for i in range(n):
		if prev >= lis[i] and lis[i] != -1:
			prev += 1
			lis[i] = -1
			c += 1
	ans += 1
	if c == n:
		break
	for i in range(n - 1, -1, -1):
		if prev >= lis[i] and lis[i] != -1:
			prev += 1
			lis[i] = -1
			c += 1
	ans += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
q = 0
c = 0
while len(a) > 0:
	de = set()
	for i in range(len(a)):
		if a[i] <= q:
			q += 1
			de.add(i)
	c += 1
	a = [a[i] for i in reversed(range(len(a))) if i not in de]
print(c - 1)


n = int(input())
data = list(map(int, input().split()))
(cnt_info, cnt_direction) = (0, 0)
while n > 0:
	if cnt_direction % 2 == 0:
		for i in range(0, len(data)):
			if data[i] != -1 and cnt_info >= data[i]:
				cnt_info += 1
				n -= 1
				data[i] = -1
	else:
		for i in range(len(data) - 1, -1, -1):
			if data[i] != -1 and cnt_info >= data[i]:
				cnt_info += 1
				n -= 1
				data[i] = -1
	cnt_direction += 1
print(cnt_direction - 1)


import sys
n = int(sys.stdin.readline())
array = [int(i) for i in sys.stdin.readline().split()]
second = [0 for i in range(n)]
modified = True
cont = 0
back = False
turn = 0
while modified == True:
	modified = False
	if not back:
		added = False
		for (i, val) in enumerate(array):
			if second[i] == 0:
				if val <= cont:
					cont += 1
					modified = True
					second[i] = 1
					if not added:
						turn += 1
						added = True
	else:
		added = False
		for (i, val) in enumerate(array[::-1]):
			if second[-i - 1] == 0:
				if val <= cont:
					cont += 1
					modified = True
					second[-i - 1] = 1
					if not added:
						turn += 1
						added = True
	back = not back
print(turn - 1)


import sys
sys.setrecursionlimit(1500)
N = int(input())
A = [int(x) for x in input().split()]
r = 0
for c in range(N + 1):
	if c % 2 == 0:
		for i in range(N):
			if A[i] >= 0 and A[i] <= r:
				r += 1
				A[i] = -1
				res = c
	else:
		for i in range(N - 1, -1, -1):
			if A[i] >= 0 and A[i] <= r:
				r += 1
				A[i] = -1
				res = c
	if r == N:
		break
print(res)


n = int(input())
pieces = list(map(int, input().split()))

def get_min(p):
	global n
	res = 0
	taken = 0
	while True:
		for i in range(n):
			if p[i] != -1 and p[i] <= taken:
				taken += 1
				p[i] = -1
		if taken == n:
			break
		res += 1
		for i in range(n - 1, -1, -1):
			if p[i] != -1 and p[i] <= taken:
				taken += 1
				p[i] = -1
		if taken == n:
			break
		res += 1
	return res
print(get_min(pieces))