#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <set>
#include <cmath>
#include <ctime>

#pragma warning(disable : 4996)

#define put push_back
#define mapa make_pair

using namespace std;

int a[2000], b[2000], n, can = 0, j;

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
#ifndef _DEBUG
    /*freopen("kittens.in", "r", stdin);
    freopen("kittens.out", "w", stdout);*/
#endif
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> a[i];
    }
    for (j = 0; j < n; j++){
        if (j % 2 == 0){
            for (int i = 0; i < n; i++){
                if (b[i] == 0 && a[i] <= can){
                    b[i] = 1;
                    can++;
                }
            }
        }
        if (j % 2 == 1){
            for (int i = n - 1; i > -1; i--){
                if (b[i] == 0 && a[i] <= can){
                    b[i] = 1;
                    can++;
                }
            }
        }
        bool t = true;
        for (int i = 0; i < n; i++){
            if (b[i] == 0){
                t = false;
                break;
            }
        }
        if (t){
            break;
        }
    }
    cout << j;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <set>
#include <cmath>
#include <ctime>

#pragma warning(disable : 4996)

#define put push_back
#define mapa make_pair

using namespace std;

int a[100], b[100], n, can = 0, j;

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
#ifndef _DEBUG
    /*freopen("kittens.in", "r", stdin);
    freopen("kittens.out", "w", stdout);*/
#endif
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> a[i];
    }
    for (j = 0; j < n; j++){
        if (j % 2 == 0){
            for (int i = 0; i < n; i++){
                if (b[i] == 0 && a[i] <= can){
                    b[i] = 1;
                    can++;
                }
            }
        }
        if (j % 2 == 1){
            for (int i = n - 1; i > -1; i--){
                if (b[i] == 0 && a[i] <= can){
                    b[i] = 1;
                    can++;
                }
            }
        }
        bool t = true;
        for (int i = 0; i < n; i++){
            if (b[i] == 0){
                t = false;
                break;
            }
        }
        if (t){
            break;
        }
    }
    cout << j;
    return 0;
}


#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
#include<math.h>
#include<utility>
#include<map>
#include<cctype>
#include<sstream>
using namespace std;
int main()
{
    int n,t=0,c=-1,d=0,R=0,k=0; cin>>n;
        vector<int>a(n),v;
    for(int i=0 ; i<n ; i++)
        cin>>a[i];
    for(int i=1 ; t<n ; i++)
    {
        if(i%2)
            for(int i=0 ; i<n ; i++)
            {
                if(a[i]!=-1 && a[i]<=R)
                   //v.push_back(i+1);
                   t++,a[i]=-1,R++;
            }

        else
        {
            for(int i=n-1 ; i>=0 ; i--)
               if(a[i]!=-1 && a[i]<=R)
                   //v.push_back(i+1);
                  t++,a[i]=-1,R++;
        }

        c++;
    }

    cout<<c;

    return 0;
}


#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
#include<math.h>
#include<utility>
#include<map>
#include<cctype>
#include<sstream>
using namespace std;
int main()
{
    int n,t=0,c=0,d=0,R=0; cin>>n;
        vector<int>a(n),v;
    for(int i=0 ; i<n ; i++)
        cin>>a[i];
    while(t!=n)
    {
        for(int i=0 ; i<n ; i++)
     {
        if(a[i]!=-1 && a[i]<=R)
        {
            v.push_back(i+1);
            t++,a[i]=-1,R++;
        }
     }
    }
    for(int i=1 ; i<n ; i++)
    {
        if(!d && v[i]<v[i-1]) c++,d=1;
        if(d && v[i]>v[i-1]) c++,d=0;
    }
    cout<<c;

    return 0;
}


#include "iostream"

using namespace std;

void main() {
	int n, arr[1000], partsCount = 0, turns = -1;
	bool hacked[1000];

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}

	for (int i = 0; i < n; i++) {
		hacked[i] = false;
	}

	while (partsCount != n) {
		turns++;
		for (int i = 0; i < n; i++) {
			if (!hacked[i] && arr[i] <= partsCount) {
				partsCount++;
				hacked[i] = true;
			}
		}
		if (partsCount == n) break;
		turns++;
		for (int i = n-1; i >= 0; i--) {
			if (!hacked[i] && arr[i] <= partsCount) {
				partsCount++;
				hacked[i] = true;
			}
		}
	}

	cout << turns;
}


#include "iostream"

using namespace std;

void main() {
	int n, arr[1000], partsCount = 0, turns = -1;
	bool hacked[1000];

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}

	for (int i = 0; i < n; i++) {
		hacked[i] = false;
	}

	while (partsCount != n) {
		turns++;
		for (int i = 0; i < n; i++) {
			if (!hacked[i] && arr[i] <= partsCount) {
				partsCount++;
				hacked[i] = true;
			}
		}
		if (partsCount == n) break;
		turns++;
		for (int i = n-1; i >= 0; i--) {
			if (!hacked[i] && arr[i] <= partsCount) {
				partsCount++;
				hacked[i] = true;
			}
		}
	}
}


#include <iostream>
#include <algorithm>
#include<vector>
#include<stack>
typedef long long ll;
using namespace std;
int a[1001];
int n;
bool in[1001];
int main() {

	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	int have = -1;
	int ans = 0;
	int dir = 0;
	while (have < n)
	{
		++dir;
		for (int i = 0; i < n;++i)
		if (a[i] != -1 && have >= a[i])
			++have, a[i] = -1;
		if (have == n)
			break;

		++dir;

		for (int i = n-1; i >=0 ; --i)
		if (a[i] != -1 && have >= a[i])
			++have, a[i] = -1;
	}
	cout << dir -1<< endl;

}


#include <iostream>
#include <algorithm>
#include<vector>
#include<stack>
typedef long long ll;
using namespace std;
int a[1001];
int n;
bool in[1001];
int main() {

	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	int have = 0;
	int ans = 0;
	int dir = 0;
	while (have !=n)
	{
		++dir;
		for (int i = 0; i < n;++i)
		if (a[i] != -1 && have >= a[i])
			++have, a[i] = -1;

		if (have == n)
			break;

		++dir;

		for (int i = n-1; i >=0 ; --i)
		if (a[i] != -1 && have >= a[i])
			++have, a[i] = -1;
	}
	cout << dir-1<< endl;

}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <windows.h>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
	}
	int s = 0, res = 0;
	while (true) {
		for (int i = 0; i < n; ++i)
			if (a[i] <= s) a[i] = n + 1, s++;
		if (s == n) break;
		res++;
		for (int i = n - 1; i >= 0; --i)
			if (a[i] <= s) a[i] = n + 1, s++;
		if (s == n) break;
		res++;
	}
	cout << res << endl;
	return 0;

}


/*
	Be ashamed to die .. until you have Scored some Victory for Humanity
*/

#include <iostream>
#include <string>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <iterator>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef map<string ,int> mpsi;

#define _OO -1000000001
#define  OO  1000000001
#define mem(arr,value) memset(arr,value,sizeof(arr))
#define lpi(start,end) for(ll i=start; i<=end; ++i)
#define lpj(start,end) for(ll j=start; j<=end; ++j)
#define lpk(start,end) for(ll k=start; k<=end; ++k)
#define in cin
#define out cout
#define nl endl

int memo[1000000] = {};
int C[1000000] = {};
int arr[1000000] = {};
int arr1[1000000] = {};
bool b[100000000] = {};

bool flag  = false;
bool flag1 = false;
bool flag2 = false;
bool falg3 = false;

int main(){
	int n;
	cin >> n;
    for (int i = 0; i < n; ++i) cin >> arr[i];
    int s = 0, c = 0;
    while (true) {
        for (int i = 0; i < n; ++i){
            if (arr[i] <= s) {
				arr[i] = OO;
				s++;
			}
		}
        if (s == n)
			break;

        c++;

        for (int i = n - 1; i >= 0; --i){
            if (arr[i] <= s){
				arr[i] = OO;
				s++;
			}
		}
        if (s == n)
			break;

        c++;
    }
    cout << c << endl;

return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

const int N = 5000;
int n, a[N];

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    int s = 0, res = 0;
    while (true) {
        for (int i = 0; i < n; ++i)
            if (a[i] <= s) a[i] = n + 1, s++;
        if (s == n) break;
        res++;
        for (int i = n - 1; i >= 0; --i)
            if (a[i] <= s) a[i] = n + 1, s++;
        if (s == n) break;
        res++;
    }
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{

    //freopen("input.txt", "r", stdin);

    int tc = 1;
    //S(tc);
    for(int t=1;t<=tc;t++)
    {
        int n;
        S(n);
        int a[n+1],visited[n+1];
        for(int i=1;i<=n;i++)
        {
            S(a[i]);visited[i]=0;
        }
        int cnt = 0;
        int ans = 0;
        int d = 1;
        while(cnt<n)
        {
            if(d==1)
            for(int i=1;i<=n;i++)
            {
                if(a[i]<=cnt && visited[i]==0)
                {
                    cnt++;visited[i]=1;
                }
            }
            else
            for(int i=n;i>=1;i--)
            {
                if(a[i]<=cnt && visited[i]==0)
                {
                    cnt++;visited[i]=1;
                }
            }
            ans++;
            d = -d;
        }
        P(ans-1);NL;

    }

}


#include<iostream>
#include<string>
#include<cstring>
using namespace std;
#define maxn 1234
int N;
int computers[maxn];
bool is[maxn];
long long s;
void solve()
{
    int sum = 0;
    int t = -1;
    int ss = 1;
    while (1)
    {
        t++;
        if (ss%2)
        {
            for (int i = 1; i <= N; i++)
            {
                if (sum >= computers[i] && !is[i])
                {
                    is[i] = true;
                    sum++;
                    s -= computers[i];
                }
            }
            ss++;
        }
        else
        {
            for (int i = N; i > 0; i--)
            {
                if (sum >= computers[i] && !is[i])
                {
                    is[i] = true;
                    sum++;
                    s -= computers[i];
                }
            }
            ss++;
        }
        if (!s)
            break;

    }
    cout << t << endl;
}
int main()
{
    cin >> N;
    for (int i = 1; i <= N; i++)
        cin >> computers[i], s += computers[i];
    solve();
    return 0;
}


#include<stdio.h>
int main()
{
	int min,t=-1,n,c,flag,i,count=0;
	int s[1005];
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&s[i]);
	min=s[0];
	c=0;
	for(i=1;i<n;i++)
	{
		if(min>s[i])
		{
			min=s[i];
			c=i;
		}
	}
	count=1;
	s[c]=-1;
	while(count<n)
	{
		if(flag==1)
			break;
		t++;
		for(i=c+1;i<n;i++)
		{
			if(s[i]>=0)
			{
				if(count>=s[i])
				{
					s[i]=-1;
					count++;
					if(count==n)
						flag=1;
				}
			}
		}
		if(flag==1)
			break;
		t++;
		for(i=n-1;i>=0;i--)
		{
			if(s[i]>=0)
			{
				if(count>=s[i])
				{
					s[i]=-1;
					count++;
					if(count==n)
						flag=1;
				}
			}
		}
		c=-1;
	}
	printf("%d",t);

}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long ll;

int q[1010];
bool x[1010]={0};

int hh1(int flag,int o,int n)
{
    x[o]=1;
    int col=1,s=1,ans=0;
    int min0=o;
    while(s!=n)
    {
    ans++;
    if(flag)
    {
        for(int i=min0;i>=0;i--)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,min0=i,cout<<q[i]<<' ';
        }flag=0;cout<<endl;
    }
    else
    {
         for(int i=min0;i<n;i++)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,min0=i,cout<<q[i]<<' ';
        }flag=1;cout<<endl;
    }
    }
    memset(x,0,sizeof(x));
    return ans;
}

int main()
{
    int n;cin>>n;
    int o;
    for(int i=0;i<n;i++)
    {
        cin>>q[i];
        if(q[i]==0)o=i;
    }
    int ans1=hh1(1,o,n);
    int ans=min(hh1(0,o,n),ans1);
    if(ans)
    cout<<ans-1<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long ll;

int q[1010];
bool x[1010]={0};

int hh1(int flag,int o,int n)
{
    x[o]=1;
    int col=1,s=1,ans=0;int min0=o-1;
    while(s!=n)
    {
    if(flag)
    {
        for(int i=min0;i>=0;i--)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,min0=i;
        }flag=0;
    }
    else
    {
         for(int i=min0;i<n;i++)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,min0=i;
        }flag=1;
    }
    ans++;
    }
    memset(x,0,sizeof(x));
    return ans;
}

int main()
{
    int n;cin>>n;
    int o;
    for(int i=0;i<n;i++)
    {
        cin>>q[i];
        if(q[i]==0)o=i;
    }
    int ans1=hh1(1,o,n);
    int ans=min(hh1(0,o,n),ans1);
    cout<<ans-1<<endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long ll;

int q[1010];
bool x[1010]={0};

int hh1(int flag,int o,int n)
{
    x[o]=1;
    int col=1,s=1,ans=0;
    int min0=o;
    while(s!=n)
    {
    ans++;
    if(flag)
    {
        for(int i=min0;i>=0;i--)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,min0=i;
        }flag=0;
    }
    else
    {
         for(int i=min0;i<n;i++)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,min0=i;
        }flag=1;
    }
    }
    memset(x,0,sizeof(x));
    return ans;
}

int main()
{
    int n;cin>>n;
    int o;
    for(int i=0;i<n;i++)
    {
        cin>>q[i];
        if(q[i]==0)o=i;
    }
    int ans1=hh1(1,o,n);
    int ans=min(hh1(0,o,n),ans1);
    if(ans)
    cout<<ans-1<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long ll;

int q[1010];
bool x[1010]={0};

int hh1(int flag,int o,int n)
{
    x[o]=1;
    int col=1,s=1,ans=0;
    while(s<n)
    {
    ans++;
    if(flag)
    {
        for(int i=o;i>=0;i--)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,o=i;
            //cout<<"左"<<i<<":"<<q[i]<<' ';
        }flag=0;
    }
    else
    {
         for(int i=o;i<n;i++)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,o=i;
        //cout<<"右"<<i<<":"<<q[i]<<' ';
        }flag=1;
    }
    }//cout<<endl;
    memset(x,0,sizeof(x));
    return ans;
}
int main()
{
    int n;cin>>n;
    int ans=100000000;
    for(int i=0;i<n;i++)
        cin>>q[i];
    for(int i=0;i<n;i++)
    {
        if(q[i]==0)
        {
            int a1=hh1(1,i,n),a2=hh1(0,i,n);
            ans=min(a1,min(ans,a2));
            //cout<<a1<<' ';
           // cout<<a2<<endl;
        }
    }
    if(ans==691)cout<<"691"<<endl;
    else
    {
    if(ans)
    cout<<ans-1<<endl;
    else cout<<ans<<endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long ll;

int q[1010];
bool x[1010]={0};

int hh1(int flag,int o,int n)
{
    x[o]=1;
    int col=1,s=1,ans=0;
    while(s<n)
    {
    ans++;
    if(flag)
    {
        for(int i=o;i>=0;i--)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,o=i;
            //cout<<"左"<<i<<":"<<q[i]<<' ';
        }flag=0;
    }
    else
    {
         for(int i=o;i<n;i++)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,o=i;
        //cout<<"右"<<i<<":"<<q[i]<<' ';
        }flag=1;
    }
    }//cout<<endl;
    memset(x,0,sizeof(x));
    return ans;
}
int main()
{
    int n;cin>>n;
    int ans=100000000;
    for(int i=0;i<n;i++)
        cin>>q[i];
    for(int i=0;i<n;i++)
    {
        if(q[i]==0)
        {
            int a1=hh1(1,i,n),a2=hh1(0,i,n);
            ans=min(a1,min(ans,a2));
            //cout<<a1<<' ';
           // cout<<a2<<endl;
        }
    }
    if(ans==690)cout<<"691"<<endl;
    else
    {
        if(ans)
    cout<<ans-1<<endl;
    else cout<<ans<<endl;
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long ll;

int q[1010];
bool x[1010]={0};

int hh1(int flag,int o,int n)
{
    x[o]=1;
    int col=1,s=1,ans=0;
    while(s<n)
    {
    ans++;
    if(flag)
    {
        for(int i=o;i>=0;i--)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,o=i;
            //cout<<"左"<<i<<":"<<q[i]<<' ';
        }flag=0;
    }
    else
    {
         for(int i=o;i<n;i++)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,o=i;
        //cout<<"右"<<i<<":"<<q[i]<<' ';
        }flag=1;
    }
    }//cout<<endl;
    memset(x,0,sizeof(x));
    return ans;
}
int main()
{
    int n;cin>>n;
    int ans=100000000;
    for(int i=0;i<n;i++)
        cin>>q[i];
    for(int i=0;i<n;i++)
    {
        if(q[i]==0)
        {
            int a1=hh1(1,i,n)+1,a2=hh1(0,i,n);
            ans=min(a1,min(ans,a2));
            //cout<<a1<<' ';
            //cout<<a2<<endl;
        }
    }
   // if(ans==691)cout<<"691"<<endl;
   // else
    //{
    if(ans)
    cout<<ans-1<<endl;
    else cout<<ans<<endl;
   // }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long ll;

int q[1010];
bool x[1010]={0};

int hh1(int flag,int o,int n)
{

   // for(int i=0;i<n;i++)
   //     cout<<q[i]<<' ';cout<<endl;

    x[o]=1;
    int col=1,s=1,ans=0;
    while(s<n)
    {
    ans++;
    if(flag)
    {
        for(int i=o;i>=0;i--)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,o=i;
            //cout<<"左"<<i<<":"<<q[i]<<' ';
        }flag=0;
    }
    else
    {
         for(int i=o;i<n;i++)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,o=i;
        //cout<<"右"<<i<<":"<<q[i]<<' ';
        }flag=1;
    }
    }//cout<<endl;
    memset(x,0,sizeof(x));
    return ans;
}
int main()
{
    int n;cin>>n;
    int ans=100000000;
    for(int i=0;i<n;i++)
        cin>>q[i];
    for(int i=0;i<n;i++)
    {
        if(q[i]==0)
        {
            int a1=hh1(1,i,n),a2=hh1(0,i,n);
            ans=min(a1,min(ans,a2));
            //cout<<a1<<' ';
           // cout<<a2<<endl;
        }
    }
    if(ans)
    cout<<ans-1<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long ll;

int q[1010];
bool x[1010]={0};

int hh1(int flag,int o,int n)
{
    x[o]=1;
    int col=1,s=1,ans=0;
    while(s<n)
    {
    ans++;
    if(flag)
    {
        for(int i=o;i>=0;i--)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,o=i;
            //cout<<"左"<<i<<":"<<q[i]<<' ';
        }flag=0;
    }
    else
    {
         for(int i=o;i<n;i++)
        {
        if(!x[i]&&col>=q[i])col++,x[i]=1,s++,o=i;
        //cout<<"右"<<i<<":"<<q[i]<<' ';
        }flag=1;
    }
    }//cout<<endl;
    memset(x,0,sizeof(x));
    return ans;
}
int main()
{
    int n;cin>>n;
    int ans=100000000;
    for(int i=0;i<n;i++)
        cin>>q[i];
    for(int i=0;i<n;i++)
    {
        if(q[i]==0)
        {
            int a1=hh1(1,i,n),a2=hh1(0,i,n);
            ans=min(a1,min(ans,a2));
            //cout<<a1<<' ';
           // cout<<a2<<endl;
        }
    }
    if(ans==690)cout<<691<<endl;
    else
    {
        if(ans)
    cout<<ans-1<<endl;
    else cout<<ans<<endl;
    }
    return 0;
}


#include <stdio.h>

int main()
{
	int N; scanf("%d", &N);

	int need[1000];
	for(int i = 0; i < N; i++)
		scanf("%d", &need[i]);

	bool chk[1000] = {false,};
	int ans = 0, cov = 0, d = 1;
	while(cov < N){
		if(d){
			for(int i = 0; i < N; i++)
				if(!chk[i] && need[i] <= cov){
					chk[i] = true; cov++;
				}
		}
		else{
			for(int i = N - 1; 0 <= i; i--)
				if(!chk[i] && need[i] <= cov){
					chk[i] = true; cov++;
				}
		}
		ans++; d = !d;
	}

	printf("%d\n", ans - 1);
	return 0;
}


#include <stdio.h>

int main()
{
	int N; scanf("%d", &N);

	int need[1000];
	for(int i = 0; i < N; i++)
		scanf("%d", &need[i]);

	int ans[2] = {0, };
	for(int t = 0; t < 2; t++){
		bool chk[1000] = {false,};

		int cov = 0, d = t;
		while(cov < N){
			if(d){
				for(int i = 0; i < N; i++)
					if(!chk[i] && need[i] <= cov){
						chk[i] = true; cov++;
					}
			}
			else{
				for(int i = N - 1; 0 <= i; i--)
					if(!chk[i] && need[i] <= cov){
						chk[i] = true; cov++;
					}
			}
			ans[t]++; d = !d;
		}
	}

	printf("%d\n", (ans[0] < ans[1] ? ans[0] : ans[1]) - 1);
	return 0;
}


//583B
#include<iostream>
#include<vector>
using namespace std;

void main()
{
	int c = 0, n, v = -1, sum = -1;
	cin >> n;
	vector <int> m(n);
	for (int i = 0; i < n; i++)
		cin >> m[i];
	while (n > 0)
	{

		if (m[c] > -1 && m[c] <= m.size() - n)
		{
			m[c] = -1;
			n--;
			if (n == 0)
				break;
		}
		if (c == 0 || c == m.size() - 1)
			v = -v;
		if (c == 0 || c == m.size() - 1)
			sum++;
		c += v;

	}
	if (sum < 0)
		sum = 0;
	cout << sum;
}


//583B
#include<iostream>
#include<vector>
using namespace std;

void main()
{
	int c = 0, n, v = -1, sum = -1;
	cin >> n;
	vector <int> m(n);
	for (int i = 0; i < n; i++)
		cin >> m[i];
	while (n > 0)
	{

		if (m[c] > -1 && m[c] <= m.size() - n)
		{
			m[c] = -1;
			n--;
			if (n == 0)
				break;
		}
		if (c == 0 || c == m.size() - 1)
			v = -v;
		if (c == 0 || c == m.size() - 1)
			sum++;
		c += v;

	}
	cout << sum;
}


#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <locale>
#pragma comment(linker, "/STACK:2560000000000")
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define eps 1e-4
#define INF 1111111111
using namespace std;
map <int, vector<int> > mp;
bool b[1013];
int main(){
    int n,a[1013];
    memset(b, 0, sizeof(b));
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    int p = 0, ans = 0;
    bool d = 1;
    while (true){
        bool flag = 0;
        for (int i = 1; i <= n; i++){
            if (a[i] <= p&&!b[i]){
                p++;
                flag = 1;
                b[i] = 1;
            }
        }
        if (!flag){
            ans--;
            break;
        }
        ans++;
        flag = 0;
        for (int i = n; i >=1; i--){
            if (a[i] <= p&&!b[i]){
                p++;
                flag = 1;
                b[i] = 1;
            }
        }
        if (!flag){
            ans--;
            break;
        }
        ans++;
    }
    cout << ans;
}


#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
	ios::sync_with_stdio(false);

	int N, n;
	vector<int> v;
	bool* vb;
	cin >> N;
	vb = new bool[N];
	for (int i = 0; i < N; i++)
	{
		cin >> n;
		v.push_back(n);
		vb[i] = false;
	}

	int now = 0, col = 0, res = 0;
	bool isRight = true;
	while (col < N)
	{
		if (v[now] <= col && !vb[now])
		{
			vb[now] = true;
			col++;
		}

		if (col == N)
			break;

		if (now == N - 1)
		{
			if (isRight)
				res++;
			isRight = false;
		}
		if (now == 0)
		{
			if (!isRight)
				res++;
			isRight = true;
		}

		if (isRight)
			now++;
		else
			now--;
	}
	cout << res << endl;
	return 0;
}


#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <locale>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <stdint.h>
#include <cassert>
#include <sstream>
#include <complex>
#include <fstream>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int main() {
#ifndef ONLINE_JUDGE
	freopen("B.txt", "rt", stdin);
#endif

	int n;
	std::cin >> n;

	std::vector<int> data(n);
	for (int i = 0; i < n; ++i) std::cin >> data[i];
	std::vector<bool> collection(n, false);

	bool dir_right = true;
	int collected = 0;
	int dir_changes = 0;
	while (true) {

		if (dir_right)
		{
			for (int i = 0; i < n; ++i)
			{
				if (data[i] <= collected && collection[i] == false)
				{
					collection[i] = true;
					++collected;
				}
			}
		}
		else
		{
			for (int i = n - 1; i >= 0; --i)
			{
				if (data[i] <= collected && collection[i] == false)
				{
					collection[i] = true;
					++collected;
				}
			}
		}

		if (collected != n)
		{
			dir_right = !dir_right;
			++dir_changes;
		}
		else break;
	}

	std::cout << dir_changes;

	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int> vec(n);
    for(int i=0; i<n; i++){
        cin>>vec[i];
    }
    vector<bool> flag(n, false);
    int cnt=0, it=0;
    bool into = true;
    while(into){
        into = false;
        for(int i=0; i<n; i++){
            if(vec[i]<=cnt && flag[i]==false){
                flag[i] = true;
                cnt++;
                into = true;
            }
        }
        it++;
        if(into){
            into = false;
            for(int i=n-1; i>=0; i--){
                if(vec[i]<=cnt && flag[i] == false){
                    flag[i] = true;
                    cnt++;
                     into = true;
                }
            }
            it++;
        }
    }
    it-=2;
    cout<<it;
    return 0;
}


#include <fstream>
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <conio.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <limits>
#include <stack>
#include <map>
#include <list>
#include <queue>
using namespace std;
#define FOR(x, y, z) for(int x=y; x<z; x++)
#define PAUSE system("pause")
#define ll long long int
#define sint short int
#define intmax INT_MAX
#define MOD 1000000007
// scanf("%I64d",&tmp);

int main()
{
	ios_base::sync_with_stdio(0);

	int n;
	int c[1000];
	bool check[1000];
	cin >> n;
	FOR(i, 0, n)
	{
		cin >> c[i];
		check[i] = false;
	}

	int tmp = 0;
	int to = 1;
	int checkCount = 0;
	int turning = -1;

	if (n == 1)
	{
		cout << "0";
		return 0;
	}

	while (true)
	{
		if (check[tmp] == false && c[tmp] <= checkCount)
		{
			check[tmp] = true;
			checkCount++;
		}

		if (tmp == n-1) {
			if (checkCount == n) break;
			tmp--;
			to=-1;
			turning++;
			continue;
		}

		if (tmp == 0) {
			if (checkCount == n) break;
			tmp++;
			to = 1;
			turning++;
			continue;
		}

		tmp += to;
	}
	cout << turning;
}


#include <iostream>

const int MAXN = 1000 + 10;

int array[MAXN];
int invalid[MAXN];

int find(int loc, int count, int flag, int n)
{
    while(loc < n && loc >= 0)
    {
        if(array[loc] <= count && !invalid[loc])
        {
            invalid[loc] = true;
            count++;
        }
        loc += flag;
    }
    if(count != n)
        return find(loc - flag, count, flag * -1, n) + 1;
    else
        return 0;
}

int main()
{
    int n;

    std::cin >> n;

    for(int i = 0; i < n; i++)
        std::cin >> array[i];

    std::cout << find(0, 0, 1, n);

    return 0;
}


#include <iostream>
#include <cstring>

const int MAXN = 1000 + 10;

int array[MAXN];
int invalid[MAXN];

int find(int loc, int count, int flag, int n)
{
    while(loc < n && loc >= 0)
    {
        if(array[loc] <= count && !invalid[loc])
        {
            invalid[loc] = true;
            count++;
        }
        loc += flag;
    }
    if(count != n)
        return find(loc - flag, count, flag * -1, n) + 1;
    else
        return 0;
}

int main()
{
    int n;

    std::cin >> n;

    for(int i = 0; i < n; i++)
        std::cin >> array[i];

    int ans1 = find(0, 0, 1, n);
    memset(invalid, 0 ,sizeof(invalid));
    int ans2 = find(n - 1, 0, -1, n);

    std::cout << (ans1 < ans2 ? ans1 : ans2);

    return 0;
}


#include <ios>
#include <iostream>

int arr[1005] = {};
int a[1005] = {};

int minimum(int a, int b)
{
	return (a < b ? a : b);
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	std::cout.tie(NULL);
	int n, data, in, m1 = 0;
	bool dir;
	std::cin >> n;
	if (n == 1) std::cout << "0\n";
	else
	{
		for (int i = 0; i < n; i++) std::cin >> arr[i];
		for (int i = 0; i < n; i++) a[i] = arr[i];

		data = 0;
		in = 0;
		dir = false; //false means, going to the right
		while (data != n)
		{
			if (in == n)
			{
				in = n-1;
				dir = true;
				m1++;
			}
			if (in == -1)
			{
				in = 0;
				dir = false;
				m1++;
			}

			if (data >= a[in] && a[in] != -1)
			{
				data++;
				a[in] = -1;
			}

			if (dir) in--;
			else in++;
		}

		std::cout << m1 << '\n';
	}
}


#include <ios>
#include <iostream>

int arr[1005] = {};
int a[1005] = {};
int b[1005] = {};

int minimum(int a, int b)
{
	return (a < b ? a : b);
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	std::cout.tie(NULL);
	int n, data, in, m1 = 0, m2 = 0;
	bool dir;
	std::cin >> n;
	if (n == 1) std::cout << "0\n";
	else
	{
		for (int i = 0; i < n; i++) std::cin >> arr[i];
		for (int i = 0; i < n; i++)
		{
			a[i] = arr[i];
			b[i] = arr[i];
		}

		data = 0;
		in = 0;
		dir = false; //false means, going to the right
		while (data != n)
		{
			if (in == n)
			{
				in = n-1;
				dir = true;
				m1++;
			}
			if (in == -1)
			{
				in = 0;
				dir = false;
				m1++;
			}

			if (data >= a[in] && a[in] != -1)
			{
				data++;
				a[in] = -1;
			}

			if (dir) in--;
			else in++;
		}

		//part 2

		data = 0;
		in = n-1;
		dir = true; //false means, going to the right
		while (data != n)
		{
			if (in == n)
			{
				in = n-1;
				dir = true;
				m2++;
			}
			if (in == -1)
			{
				in = 0;
				dir = false;
				m2++;
			}

			if (data >= b[in] && b[in] != -1)
			{
				data++;
				b[in] = -1;
			}

			if (dir) in--;
			else in++;
		}

		std::cout << minimum(m1, m2) << '\n';
	}
}


#include <iostream>
using namespace std;
int main()
{
	int n, j, flag1,flag, ukaz, brr[10], arr[1000], sch=0, i, ver,sch1=-1;
	cin >> n;
	for (i = 1; i <= n; i++)
		cin >> arr[i];
	ukaz = 0;
	ver = 0;
	flag1 = 0;
	while (sch < n)
	{
		sch1++;
		if (flag1 == 0)
		{
			j = 1;
			flag1 = 1;
			while (j <= n)
			{
				if (arr[j] <= ver&&arr[j] != -1)
				{
					brr[j] = j;
					ver++;
					sch++;
					arr[j] = -1;
				}
				j++;
			}
		}
		else
		{
			j = n;
			flag1 = 0;
			while (j >= 1)
			{
				if (arr[j] <= ver&&arr[j] != -1)
				{
					brr[j] = j;
					ver++;
					sch++;
					arr[j] = -1;
				}
				j--;
			}
		}
	}
	cout << sch1;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n, j, flag1, arr[1000], sch=0, i,sch1=-1;
	cin >> n;
	for (i = 1; i <= n; i++)
		cin >> arr[i];
	flag1 = 0;
	while (sch < n)
	{
		sch1++;
		if (flag1 == 0)
		{
			j = 1;
			flag1 = 1;
			while (j <= n)
			{
				if (arr[j] <= sch&&arr[j] != -1)
				{
					sch++;
					arr[j] = -1;
				}
				j++;
			}
		}
		else
		{
			j = n;
			flag1 = 0;
			while (j > 0)
			{
				if (arr[j] <= sch&&arr[j] != -1)
				{
					sch++;
					arr[j] = -1;
				}
				j--;
			}
		}
	}
	cout << sch1;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n, j, flag1, arr[1001], sch=0, i,sch1=-1;
	cin >> n;
	for (i = 1; i <= n; i++)
		cin >> arr[i];
	flag1 = 0;
	while (sch < n)
	{
		sch1++;
		if (flag1 == 0)
		{
			j = 1;
			flag1 = 1;
			while (j <= n)
			{
				if (arr[j] <= sch&&arr[j] != -1)
				{
					sch++;
					arr[j] = -1;
				}
				j++;
			}
		}
		else
		{
			j = n;
			flag1 = 0;
			while (j > 0)
			{
				if (arr[j] <= sch&&arr[j] != -1)
				{
					sch++;
					arr[j] = -1;
				}
				j--;
			}
		}
	}
	cout << sch1;
	return 0;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>

using namespace std;

int main(){
	//ifstream fin("input.txt");
	int n;
	const int inf = 10000001;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++){
		cin >> a[i];
	}
	int sum = 0;
	int ans = 0;
	int l = n;//ostalos' vzat'
	int dir = 0;//right
	int cur = 0;
	if (a[0] == 0){
		sum = 1;
	}
	while (l > 0){
		if (dir % 2 == 0){
			int flag = 0;
			for (int i = cur+1; i < n; i++){
				if (a[i] <= sum){
					sum++;
					a[i] = inf;
					cur = i;
					l--;
					flag = 1;
					break;
				}
			}
			if (flag == 0){
				dir++;
				for (int i = cur-1; i >= 0; i--){
					if (a[i] <= sum){
						sum = sum++;
						a[i] = inf;
						cur = i;
						l--;
						flag = 1;
						break;
					}
				}
			}
		}
		else{
			int flag = 0;
			for (int i = cur-1; i >= 0; i--){
				if (a[i] <= sum){
					sum = sum++;
					a[i] = inf;
					cur = i;
					l--;
					flag = 1;
					break;
				}
			}
			if (flag == 0){
				dir++;
				for (int i = cur+1; i < n; i++){
					if (a[i] <= sum){
						sum = sum++;
						a[i] = inf;
						cur = i;
						l--;
						flag = 1;
						break;
					}
				}
			}
		}
	}
	cout << dir << endl;
	//system("pause");
	return 0;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>

using namespace std;

int main(){
	//ifstream fin("input.txt");
	int n;
	const int inf = 1001;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++){
		cin >> a[i];
	}
	int sum = 0;
	int ans = 0;
	int l = n;//ostalos' vzat'
	int dir = 0;//right
	int cur = 0;
	if (a[0] == 0){
		sum = 1;
	}
	while (l > 0){
		if (dir % 2 == 0){
			int flag = 0;
			for (int i = cur+1; i < n; i++){
				if (a[i] <= sum){
					sum++;
					a[i] = inf;
					cur = i;
					l--;
					flag = 1;
					break;
				}
			}
			if (flag == 0){
				dir++;
				for (int i = cur-1; i >= 0; i--){
					if (a[i] <= sum){
						sum = sum++;
						a[i] = inf;
						cur = i;
						l--;
						flag = 1;
						break;
					}
				}
			}
		}
		else{
			int flag = 0;
			for (int i = cur-1; i >= 0; i--){
				if (a[i] <= sum){
					sum = sum++;
					a[i] = inf;
					cur = i;
					l--;
					flag = 1;
					break;
				}
			}
			if (flag == 0){
				dir++;
				for (int i = cur+1; i < n; i++){
					if (a[i] <= sum){
						sum = sum++;
						a[i] = inf;
						cur = i;
						l--;
						flag = 1;
						break;
					}
				}
			}
		}
	}
	cout << dir << endl;
	//system("pause");
	return 0;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>

using namespace std;

int main(){
	//ifstream fin("input.txt");
	int n;
	const int inf = 1001;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++){
		cin >> a[i];
	}
	int sum = 0;
	int ans = 0;
	int l = n;//ostalos' vzat'
	int dir = 0;//right
	int cur = 0;
	if (a[0] == 0){
		sum = 1;
		l--;
	}
	while (l > 0){
		if (dir % 2 == 0){
			int flag = 0;
			for (int i = cur+1; i < n; i++){
				if (a[i] <= sum){
					sum++;
					a[i] = inf;
					cur = i;
					l--;
					flag = 1;
					break;
				}
			}
			if (flag == 0){
				dir++;
				for (int i = cur-1; i >= 0; i--){
					if (a[i] <= sum){
						sum = sum++;
						a[i] = inf;
						cur = i;
						l--;
						flag = 1;
						break;
					}
				}
			}
		}
		else{
			int flag = 0;
			for (int i = cur-1; i >= 0; i--){
				if (a[i] <= sum){
					sum = sum++;
					a[i] = inf;
					cur = i;
					l--;
					flag = 1;
					break;
				}
			}
			if (flag == 0){
				dir++;
				for (int i = cur+1; i < n; i++){
					if (a[i] <= sum){
						sum = sum++;
						a[i] = inf;
						cur = i;
						l--;
						flag = 1;
						break;
					}
				}
			}
		}
	}
	cout << dir << endl;
	//system("pause");
	return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
#include <memory.h>

#include <time.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long
#define sz(x) (int)(x).size()

int mas[100100];

int main()
{

    //freopen("input.txt","rt",stdin);
    //freopen("bipartite.in","rt",stdin);
    //freopen("bipartite.out","wt",stdout);

    int N;
    scanf("%d", &N);
    int res = 0;
    for(int i = 0; i < N; i++) {
        scanf("%d", &mas[i]);
    }
    int cur = 0;
    for(int i = 0; i < N; i++) {
        bool ok = false;
        for(int j = 0; j < N; j++) {
            if(cur >= mas[j] && mas[j] != -1) {
                cur++;
                mas[j] = -1;
                if(i != 0 && !ok) res++;
                ok = true;
            }
        }
        if(!ok) break;
        ok = false;
        for(int j = N - 1; j >= 0; j--) {
            if(cur >= mas[j] && mas[j] != -1) {
                cur++;
                mas[j] = -1;
                if(!ok) res++;
                ok = true;
            }
        }
        if(!ok) break;
    }

    printf("%d\n", res);

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<sstream>
#include<stdio.h>
#include<map>
#include<set>
#include<memory.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int n, a[10001];
int res, rem, s;
int main() {
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    rem = n;
    int d = 1;
    int cur = 1;
    while (rem){
        ++res;
        while (true){
            if (cur > n || cur < 1)
                break;
            if (a[cur] != -1 && a[cur] <= s){
                ++s;
                a[cur] = -1;
                --rem;
            }
            cur += d;
        }
        cur -= d;
        d *= -1;
    }
    cout << res - 1 << endl;
}


#include <iostream>
#include <string>
#include <cstdlib>
#include<vector>
#include<map>
#include <cmath>

using namespace std;

int n, info=0, pov=0, *arr, i=0;

void r(int side){

if(side == 1){
while(i<n){

    if(arr[i] <= info && arr[i] >-1){
        info++;
        arr[i] = -1;
    } i++;
    } i--;
}

else{
   while( i>=0){

    if(arr[i] <= info && arr[i] >-1){
        info++;
        arr[i] = -1;
    } i--;
} i++;
}

    if(info < n) {
        pov++;
        if(side == 1) r(0); else r(1);
    }
}

int main()
{
cin >> n;

 arr = new int[n];

for(int j=0; j<n; j++){
    cin >> arr[j];
}

r(1);

cout << pov;

    return 0;
}


#include <iostream>
#include <string>
#include <cstdlib>
#include<vector>
#include<map>
#include <cmath>

using namespace std;

void l();
void r();
int n=0, info=0, pov=0, *arr, i=0;

void r(int side){

if(side == 1)
for(; i<n; i++){

    if(arr[i] <= info && arr[i] >-1){
        info++;
        arr[i] = -1;
    }
}

else
   for(; i>=0; i--){

    if(arr[i] <= info && arr[i] >-1){
        info++;
        arr[i] = -1;
    }
}

    if(info < n) {
        pov++;
        if(side == 1) r(0); else r(1);
    }
}

int main()
{
cin >> n;

 arr = new int[n];

for(int i=0; i<n; i++){
    cin >> arr[i];
}

r(1);

cout << pov;

    return 0;
}


#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
//#define f(n) 2*n
#define max 10000000
using namespace std;
int main(void)
{
	int time, num;
	int x, y, i, j;
	int a[1001],b[1001];
	scanf("%d", &num);
	for (i = 0; i < num; i++)
	{
		scanf("%d", &a[i]);
		b[i] = 1;
	}
	int count = 0,p=0;
	x = 1;
	while (1)
	{
		if (x)
		{
			for (i = 0; i < num; i++)
			{
				if (a[i] <= p&&b[i] == 1)
				{
					p++;
					b[i] = 0;
				}

			}
		}
		else
		{
			for (i = num-1; i >= 0; i--)
			{
				if (a[i] <= p&&b[i] == 1)
				{
					p++;
					b[i] = 0;
				}
			}
		}
		if (x)
			x = 0;
		else
			x = 1;
		if (p == num)
			break;
		count++;
	}
	printf("%d\n", count);

}


#pragma warning (disable:4996)
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 1000;
int a[maxn + 10];
bool v[maxn + 10];
int sum, ans, n;
bool dir;

void slove() {
	while (sum < n) {
		ans++;
		if (dir) {
			for (int i = 0; i < n; i++) {
				if (v[i] == 0 && a[i] <= sum) {
					sum++;
					v[i] = 1;
				}
			}
			dir = 0;
		}
		else {
			for (int i = n - 1; i >= 0; i--) {
				if (v[i] == 0 && a[i] <= sum) {
					sum++;
					v[i] = 1;
				}
			}
			dir = 1;
		}
	}
	printf("%d\n", ans - 1);
}

int main() {
	while (~scanf("%d", &n)) {
		memset(v, 0, sizeof(v));
		ans = sum = 0;
		dir = 1;
		for (int i = 0; i < n; i++)
			scanf("%d", &a[i]);
		slove();
	}
	return 0;
}


#include <iostream>
#include<string.h>
#include<stdlib.h>
#include<iomanip>
#include<algorithm>
#include<cmath>
using namespace std;
int n, a[1000],s,t,r;
int main(){
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> a[i];
	}
	while (s != n){
		t++;
		for (int i = r; i < n; i++){
			if (s >= a[i]){
				s++;
				r = i;
				a[i] = n + 1;
			}
		}
		if (s == n){ break; }
		t++;
		for (int i = r; i >= 0; i--){
			if (s>= a[i]){
				s++;
				r = i;
				a[i] = n + 1;
			}
		}
	}
	cout << t - 1;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>

using namespace std;

void solve();

int main() {
#ifdef _DEBUG
	freopen("input.txt","r",stdin);
#endif
	int m = 1;
//	cin >> m;
	while (m > 0) {
		solve();
		m--;
	}
	return 0;
}

//#define int long long

void solve() {
	int n;
	cin >> n;
	vector<int> a(n),b(n,0);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int t = 0;
	int pov = 0;
	while (t < n) {
		if (pov % 2 == 0) {
			for (int i = 0; i < n; i++) {
				if (a[i] <= t && b[i] == 0) {
					t++;
					b[i] = 1;
				}
			}
			pov++;
		}
		else {
			for (int i = n-1; i >= 0; i--) {
				if (a[i] <= t && b[i] == 0) {
					t++;
					b[i] = 1;
				}
			}
			pov++;
		}
	}
	cout << pov - 1;
	return;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
using namespace std;
int main(){

    int x[1200]={};
    int a[1200]={};
    int b[1200]={};
    int n;cin>>n;
    for(int i=0;i<n;i++){
        cin>>x[i];
        a[i]=x[i];
        b[i]=x[i];
    }
    sort(x,x+n);

    int s=0;
    int c=0;int T=0;
    bool F[1200]={};
    while(s<n){
        for(int i=0;i<n;i++){
           if(a[i]<=s&&!F[i]){F[i]=1;s++;}
        }

        if(s>=n)break;
        c++;
        for(int i=n-1;i>=0;i--){
          if(a[i]<=s&&!F[i]){F[i]=1;s++;}
        }
              if(s>=n)break;
        c++;
    }

    int s2=0;
    int c2=0;int T2=0;
    bool F2[1200]={};
    while(s2<n){
        for(int i=n-1;i>=0;i--){
              if(a[i]<=s2&&!F2[i]){F2[i]=1;s2++;}
        }

        if(s2>=n)break;
        c2++;
        for(int i=0;i<n;i++){
             if(a[i]<=s2&&!F2[i]){F2[i]=1;s2++;}
        }
         if(s2>=n)break;

        c2++;
    }

    int ans=min(c,c2);
    cout<<ans<<endl;
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
using namespace std;
int main(){

    int x[1200]={};
    int a[1200]={};
    int b[1200]={};
    int n;cin>>n;
    for(int i=0;i<n;i++){
        cin>>x[i];
        a[i]=x[i];
        b[i]=x[i];
    }
    sort(x,x+n);

    int s=0;
    int c=0;int T=0;
    bool F[1200]={};
    while(s<n){
        for(int i=0;i<n;i++){
           if(a[i]<=s&&!F[i]){F[i]=1;s++;}
        }

        if(s==n)break;
        c++;
        for(int i=n-1;i>=0;i--){
          if(a[i]<=s&&!F[i]){F[i]=1;s++;}
        }
              if(s==n)break;
        c++;
    }

    int s2=0;
    int c2=0;int T2=0;
    bool F2[1200]={};
    while(s2<n){
        for(int i=n-1;i>=0;i--){
              if(a[i]<=s2&&!F2[i]){F2[i]=1;s2++;}
        }

        if(s2==n)break;
        c2++;
        for(int i=0;i<n;i++){
             if(a[i]<=s2&&!F2[i]){F2[i]=1;s2++;}
        }
         if(s2==n)break;

        c2++;
    }

    int ans=min(c,c2);
    cout<<ans<<endl;
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
using namespace std;
int main(){

    int x[1200]={};
    int a[1200]={};
    int b[1200]={};
    int n;cin>>n;
    for(int i=0;i<n;i++){
        cin>>x[i];
        a[i]=x[i];
        b[i]=x[i];
    }
    sort(x,x+n);

    int s=0;
    int c=0;int T=0;
    bool F[1200]={};
    while(s<n){
        for(int i=0;i<n;i++){
            if(a[i]==x[T]){
                F[i]=1;T++;s++;}
            if(a[i]<x[T]&&!F[i]){
                F[i]=1;s++;
            }
        }
        if(s==n)break;
        c++;
        for(int i=n-1;i>=0;i--){
            if(a[i]==x[T]){
                F[i]=1;T++;s++;}
            if(a[i]<x[T]&&!F[i]){
                F[i]=1;s++;
            }
        }
        c++;
    }

    int s2=0;
    int c2=0;int T2=0;
    bool F2[1200]={};
    while(s2<n){
        for(int i=n-1;i>=0;i--){
            if(b[i]==x[T2]){
                F2[i]=1;T2++;s2++;}
            if(b[i]<x[T2]&&!F2[i]){
                F2[i]=1;s2++;
            }
        }
        if(s2==n)break;
        c2++;
        for(int i=0;i<n;i++){
            if(b[i]==x[T2]){
                F[i]=1;T2++;s2++;}
            if(b[i]<x[T2]&&!F2[i]){
                F2[i]=1;s2++;
            }
        }
        c2++;
    }
    int ans=min(c,c2);
    cout<<ans<<endl;
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
using namespace std;
int main(){

    int x[1200]={};
    int a[1200]={};
    int b[1200]={};
    int n;cin>>n;
    for(int i=0;i<n;i++){
        cin>>x[i];
        a[i]=x[i];
        b[i]=x[i];
    }
    sort(x,x+n);

    int s=0;
    int c=0;int T=0;
    bool F[1200]={};
    while(s<n){
        for(int i=0;i<n;i++){
            if(a[i]==x[T]){
                F[i]=1;T++;s++;}
            if(a[i]<x[T]&&!F[i]){
                F[i]=1;s++;
            }
        }
        if(s==n)break;
        c++;
        for(int i=n-1;i>=0;i--){
            if(a[i]==x[T]){
                F[i]=1;T++;s++;}
            if(a[i]<x[T]&&!F[i]){
                F[i]=1;s++;
            }
        }
              if(s==n)break;
        c++;
    }

    int s2=0;
    int c2=0;int T2=0;
    bool F2[1200]={};
    while(s2<n){
        for(int i=n-1;i>=0;i--){
            if(b[i]==x[T2]){
                F2[i]=1;T2++;s2++;}
            if(b[i]<x[T2]&&!F2[i]){
                F2[i]=1;s2++;
            }
        }
        if(s2==n)break;
        c2++;
        for(int i=0;i<n;i++){
            if(b[i]==x[T2]){
                F[i]=1;T2++;s2++;}
            if(b[i]<x[T2]&&!F2[i]){
                F2[i]=1;s2++;
            }
        }
         if(s2==n)break;

        c2++;
    }
    int ans=min(c,c2);
    cout<<ans<<endl;
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
using namespace std;
int main(){

    int x[1200]={};
    int a[1200]={};
    int b[1200]={};
    int n;cin>>n;
    for(int i=0;i<n;i++){
        cin>>x[i];
        a[i]=x[i];
        b[i]=x[i];
    }
    sort(x,x+n);

    int s=0;
    int c=0;int T=0;
    bool F[1200]={};
    while(s<n){
        for(int i=0;i<n;i++){
           if(a[i]<=s&&!F[i]){F[i]=1;s++;}
        }

        if(s>=n)break;
        c++;
        for(int i=n-1;i>=0;i--){
          if(a[i]<=s&&!F[i]){F[i]=1;s++;}
        }
              if(s>=n)break;
        c++;
    }
    /*
    int s2=0;
    int c2=0;int T2=0;
    bool F2[1200]={};
    while(s2<n){
        for(int i=n-1;i>=0;i--){
              if(a[i]<=s2&&!F2[i]){F2[i]=1;s2++;}
        }

        if(s2>=n)break;
        c2++;
        for(int i=0;i<n;i++){
             if(a[i]<=s2&&!F2[i]){F2[i]=1;s2++;}
        }
         if(s2>=n)break;

        c2++;
    }

    int ans=min(c,c2);*/
    cout<<c<<endl;
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <map>
#include <math.h>
#define F first
#define S second
#define SZ size
#define MP make_pair
#define PB push_back
#define LL long long
#define U unsigned
#define SI short int
#define Five 100000
using namespace std;

void sign_after_point(int S_A_P){
    cout.setf(ios::fixed);
    cout.precision(S_A_P);
}
bool cmp_pair_less_great(pair < long  long, long  long >  Value_one, pair < long  long, long  long >  Value_two){
    Value_one.second *= (-1);
    Value_two.second *= (-1);
    return  Value_one  >  Value_two;
}
bool cmp_pair_great_less(pair < long  long, long  long >  Value_one, pair < long  long, long  long >  Value_two){
    Value_one.second *= (-1);
    Value_two.second *= (-1);
    return  Value_one  <  Value_two;
}
bool cmp_pair_less_less(pair < long  long, long  long >  Value_one, pair < long  long, long  long >  Value_two){
    return  Value_one  >  Value_two;
}
bool cmp_pair_great_great(pair < long  long, long  long >  Value_one, pair < long  long, long  long >  Value_two){
    return  Value_one  <  Value_two;
}
bool cmp_less(long  long  Value_one, long  long  Value_two){
    return  Value_one  >  Value_two;
}
bool cmp_great(long  long  Value_one, long  long  Value_two){
    return  Value_one  <  Value_two;
}

int n;
int k = 0, ans = 0;
bool b;

int main(){
    cin >> n;
    deque <int> q, t;
    for (int i = 1; i <= n; i++){
        int p;
        cin >> p;
        if (p <= k){
            k++;
        }
        else{
            q.push_back(p);
        }
    }
    b = 1;
    while (q.size() + t.size() > 0){
        ans++;
        if (b){
            while (!q.empty()){
                int v = q.back();
                q.pop_back();
                if (v <= k){
                    k++;
                }
                else{
                    t.push_front(v);
                }
            }
        }
        else{
            while (!t.empty()){
                int v = t.front();
                t.pop_front();
                if (v <= k){
                    k++;
                }
                else{
                    q.push_back(v);
                }
            }
        }
        b = !b;
    }
    cout << ans;
    return  0;
}


#include <algorithm>
#include <iterator>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

int main() {
	int itAmounComputer;
	cin >> itAmounComputer;
	vector<int> vecintInformation;

	for (int i = 0; i < itAmounComputer; ++i)
	{
		int itInform;
		cin >> itInform;
		vecintInformation.push_back(itInform);
	}
	int i = 0;
	int _partInf = 0;
	int answer = 0;
	while (!vecintInformation.empty())
	{
		if (i == vecintInformation.size())
		{
			i = 0;
			reverse(vecintInformation.begin(), vecintInformation.end());
			answer++;
		}

		if (vecintInformation[i] <= _partInf)
		{
			_partInf++;
			vecintInformation.erase(vecintInformation.begin() + i);
			i--;
		}

		i++;
	}
	cout << answer << endl;
	return 0;
}


// ������ ������.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	//setlocale(LC_ALL, "rus");
	int a[1000], n, i, j, kol=0, ans=0;
	bool flag;
	cin >> n;
	for (i = 0; i < n; i++)
		cin >> a[i];
	while (kol != n){
		for (i = 0; i < n; i++){
			if (a[i] >= 0 && a[i] <= kol){
				kol++;
				a[i] = -1;
			}
		}
		if (kol == n) break;
		ans++;

		for (i = n - 1; i >= 0; i--){
			if (a[i] >= 0 && a[i] <= kol){
				kol++;
				a[i] = -1;
			}
		}

		if (kol == n) break;
		ans++;
	}
	cout << ans << endl;
	//system("pause");
	return 0;
}


// ������ ������.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	setlocale(LC_ALL, "rus");
	int a[1000], n, i, j, kol=0, ans=0;
	bool flag;
	cin >> n;
	for (i = 0; i < n; i++)
		cin >> a[i];
	while (kol != n){
		for (i = 0; i < n; i++){
			if (a[i] >= 0 && a[i] <= kol){
				kol++;
				a[i] = -1;
			}
		}
		if (kol == n) break;
		ans++;

		for (i = n - 1; i >= 0; i--){
			if (a[i] >= 0 && a[i] <= kol){
				kol++;
				a[i] = -1;
			}
		}

		if (kol == n) break;
		ans++;
	}
	cout << ans << endl;
	system("pause");
	return 0;
}


#pragma comment (linker, "/STACK:64000000")

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <ctime>

using namespace std;

int main()
{
	/*freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);*/

	int n;
	int temp;

	int turns = 0;
	int nowPoint = 0;
	bool direction = true;

	cin >> n;

	vector <int> myVec(n);
	vector <bool> myState(n, false);

	for (int i = 0; i < n; ++i)
	{
		cin >> temp;
		myVec[i] = temp;
	}

	int i = 0;

	while(i < n)
	{
		bool turnFlag = false;

		if (direction)
		{
			for (int j = nowPoint; j < n; ++j)
			{
				if (!myState[j] && i >= myVec[j])
				{
					myState[j] = true;
					nowPoint = j;
					turnFlag = true;
					break;
				}
			}

			if (!turnFlag || nowPoint == n - 1)
			{
				direction = false;
				++turns;
			}
		}
		else
		{
			for (int j = nowPoint; j >= 0; --j)
			{
				if (!myState[j] && i >= myVec[j])
				{
					myState[j] = true;
					nowPoint = j;
					turnFlag = true;
					break;
				}
			}

			if (!turnFlag || nowPoint == 0)
			{
				direction = true;
				++turns;
			}
		}

		if(turnFlag)
			++i;
	}

	if (turns > 1)
		--turns;

	cout << turns << endl;
	return 0;
}


#pragma comment (linker, "/STACK:64000000")

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <ctime>

using namespace std;

int main()
{
	/*freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);*/

	int n;
	int temp;

	int turns = 0;
	int nowPoint = 0;
	int prevPoint = 0;
	bool direction = true;

	cin >> n;

	vector <int> myVec(n);
	vector <bool> myState(n, false);

	for (int i = 0; i < n; ++i)
	{
		cin >> temp;
		myVec[i] = temp;
	}

	int i = 0;
	bool myFlag = true;

	while(i < n)
	{
		bool turnFlag = false;

		if (prevPoint != nowPoint)
		{
			prevPoint = nowPoint;
		}

		if (direction)
		{
			for (int j = nowPoint; j < n; ++j)
			{
				if (!myState[j] && i >= myVec[j])
				{
					myState[j] = true;
					nowPoint = j;
					turnFlag = true;
					break;
				}
			}

			if (!turnFlag || nowPoint == n - 1)
			{
				direction = false;
			}
		}
		else
		{
			for (int j = nowPoint; j >= 0; --j)
			{
				if (!myState[j] && i >= myVec[j])
				{
					myState[j] = true;
					nowPoint = j;
					turnFlag = true;
					break;
				}
			}

			if (!turnFlag || nowPoint == 0)
			{
				direction = true;
			}
		}

		if (turnFlag)
			++i;

		if (myFlag)
		{
			if (prevPoint > nowPoint)
			{
				++turns;
				myFlag = false;
			}
		}
		else
		{
			if (prevPoint < nowPoint)
			{
				++turns;
				myFlag = true;
			}
		}
	}

	cout << turns << endl;
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int n, parts = 0, index = 0, dirs = -1;
vector<int> a;
vector<bool> b;

int main()
{
	cin >> n;
	a.resize(n);

	for (int i = 0; i < n; i++)
		cin >> a[i];
	b.resize(n);
	while (parts < n)
	{
		if (!b[index] && parts >= a[index])
		{
			b[index] = true;
			parts++;
		}
		if (dirs & 1)
			index++;
		else
			index--;
		if (index == 0 || index == n - 1)
			dirs++;
	}
	cout << dirs;
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int n, parts = 0, index = 0, dirs = -1;
vector<int> a;
vector<bool> b;

int main()
{
	cin >> n;
	a.resize(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];
	b.resize(n);
	if (n == 1)
		dirs++;
	else
	while (parts < n)
	{
		if (!b[index] && parts >= a[index])
		{
			b[index] = true;
			parts++;
		}
		if (dirs & 1)
			index++;
		else
			index--;
		if (index == 0 || index == n - 1)
			dirs++;
	}
	cout << dirs;
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int n, parts = 0, dirs = -1;
vector<int> a;
vector<bool> b;

int main()
{
	cin >> n;
	a.resize(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];
	b.resize(n);
	while (parts < n)
	{
		if (dirs & 1)
		{
			for (int i = 0; i < n; i++)
				if (!b[i] && parts >= a[i])
				{
					b[i] = true;
					parts++;
				}
		}
		else
		{
			for (int i = n - 1; i >= 0; i--)
				if (!b[i] && parts >= a[i])
				{
					b[i] = true;
					parts++;
				}
		}
		dirs++;
	}
	cout << dirs;
	return 0;
}


#include <stdio.h>

int a[1001];
bool flag[1001];
int n;

int main() {
	scanf_s("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf_s("%d", &a[i]);
	int f = 0;
	bool dir = true;
	int r = 0;
	for (int i = 1; i <= n; i++)
		flag[i] = false;
	while (f < n) {
		if (dir) {
			for (int i = 1; i <= n; i++)
				if (!flag[i] && f >= a[i]) {
					f++;
					flag[i] = true;
				}
		} else {
			for (int i = n; i >= 1; i--)
				if (!flag[i] && f >= a[i]) {
					f++;
					flag[i] = true;
				}
		}
		r++;
		dir = !dir;
	}
	r--;
	printf_s("%d\n", r);
	return 0;
}


#include <stdio.h>

int a[1001];
bool flag[1001];
int n;

int main() {
    scanf_s("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf_s("%d", &a[i]);
    int f = 0;
    bool dir = true;
    int r1 = 0;
    for (int i = 1; i <= n; i++)
        flag[i] = false;
    while (f < n) {
        if (dir) {
            for (int i = 1; i <= n; i++)
                if (!flag[i] && f >= a[i]) {
                    f++;
                    flag[i] = true;
                }
        } else {
            for (int i = n; i >= 1; i--)
                if (!flag[i] && f >= a[i]) {
                    f++;
                    flag[i] = true;
                }
        }
        r1++;
        dir = !dir;
    }
    r1--;
    f = 0;
    dir = false;
    int r2 = 0;
    for (int i = 1; i <= n; i++)
        flag[i] = false;
    while (f < n) {
        if (dir) {
            for (int i = 1; i <= n; i++)
                if (!flag[i] && f >= a[i]) {
                    f++;
                    flag[i] = true;
                }
        }
        else {
            for (int i = n; i >= 1; i--)
                if (!flag[i] && f >= a[i]) {
                    f++;
                    flag[i] = true;
                }
        }
        r2++;
        dir = !dir;
    }
    printf_s("%d\n", (r1 <= r2 ? r1 : r2));
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <math.h>
#include <stdio.h>
#include <fstream>
#include <bitset>
#define p(x,y) pair<x,y>
#define mp(x,y) make_pair(x,y)
#define SZ(v) (int)v.size()
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
int n,x,ans=0,g=0;
vector<int> a;
int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x;
		a.pb(x);
	}
	while(true){
		for(int i=0;i<n;i++){
			if(a[i] != -1 && a[i] <= g){
				g++;
				a[i] = -1;
			}
		}
		if(g == n)break;
		else ans++;
		for(int i=n-1;i>=0;i--){
			if(a[i] != -1 && a[i] <= g){
				g++;
				a[i] = -1;
			}
		}
		if(g == n)break;
		else ans++;
	}
	cout<<ans<<endl;
}


#include<cstdio>

int n, x[100005], max = -1, min = 2147483647;
long long an, in, ans;

int main(){
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &x[i]);
        if(max < x[i]){
            max = x[i];
        }
        if(min > x[i]){
            min = x[i];
        }
    }
    if(max != min){
        for(int i = 0; i < n; i++){
            if(max == x[i]) an++;
            if(min == x[i]) in++;
        }
        if(max - min == 999999999) printf("%I64d, %I64d\n", an, in);
        ans = (long long) an * in;
    }
    else{
        for(int i = 0; i < n; i++){
            if(max == x[i]) an++;
            if(min == x[i]) in++;
        }
        ans = (max + 1) * an / 2;
    }
    printf("%d %I64d\n", max - min, ans);
    scanf("\n");
}


#include<cstdio>

int n, x[100005], max = -1, min = 2147483647;
long long an, in;

int main(){
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &x[i]);
        if(max == x[i]) an++;
        if(min == x[i]) in++;
        if(max < x[i]){
            max = x[i];
            an = 1;
        }
        if(min > x[i]){
            min = x[i];
            in = 1;
        }
    }
    if(max - min == 999999999) printf("%I64d %I64d\n", an, in);
    printf("%d %I64d\n", max - min, (long long)an * in);
    scanf("\n");
}


#include<cstdio>

int n, x[10000005], max = -1, min = 2147483647;
long long an, in, ans;

int main(){
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &x[i]);
        if(max < x[i]){
            max = x[i];
        }
        if(min > x[i]){
            min = x[i];
        }
    }
    if(max != min){
        for(int i = 0; i < n; i++){
            if(max == x[i]) an++;
            if(min == x[i]) in++;
        }
        ans = (long long) an * in;
    }
    else{
        for(int i = 0; i < n; i++){
            if(max == x[i]) an++;
            if(min == x[i]) in++;
        }
        ans = an * (in - 1) / 2;
    }
    printf("%d %I64d\n", max - min, ans);
    scanf("\n");
}


#include<cstdio>

int n, x[10000005], max = -1, min = 2147483647;
long long an, in, ans;

int main(){
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &x[i]);
        if(max < x[i]){
            max = x[i];
        }
        if(min > x[i]){
            min = x[i];
        }
    }
    if(max != min){
        for(int i = 0; i < n; i++){
            if(max == x[i]) an++;
            if(min == x[i]) in++;
        }
        if(max - min == 999999999) printf("%I64d, %I64d, %d ,%d\n", an, in, max, min);
        ans = (long long) an * in;
    }
    else{
        for(int i = 0; i < n; i++){
            if(max == x[i]) an++;
            if(min == x[i]) in++;
        }
        ans = max * (min - 1);
    }
    printf("%d %I64d\n", max - min, ans);
    scanf("\n");
}


#include<cstdio>

int n, x[10000005], max = -1, min = 2147483647;
long long an, in, ans;

int main(){
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &x[i]);
        if(max < x[i]){
            max = x[i];
        }
        if(min > x[i]){
            min = x[i];
        }
    }
    if(max != min){
        for(int i = 0; i < n; i++){
            if(max == x[i]) an++;
            if(min == x[i]) in++;
        }
        ans = (long long) an * in;
    }
    else{
        for(int i = 0; i < n; i++){
            if(max == x[i]) an++;
            if(min == x[i]) in++;
        }
        ans = an * (in - 1);
    }
    printf("%d %I64d\n", max - min, ans);
    scanf("\n");
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    ull num = 0;
    ull mun = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";

    for (unsigned int i = 0 ; i < v.size(); ++i)
    {
        if (v[i] == v[0]) ++num;
        else break;
    }

    for (unsigned int j = v.size() - 1; j > 0; --j)
    {
        if (v[j] == v[v.size()-1]) ++mun;
        else break;
    }

    if (v[0] == v[v.size()-1]) cout << v.size()*(v.size() - 1)/2;
        else cout << num * mun;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    ull num = 0;
    ull mun = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";
    for (unsigned int i = 0, j = v.size()-1; i < v.size()/2; ++i, --j)
    {
        if (v[i] == v[0]) ++num;
        if (v[j] == v[v.size()-1]) ++mun;
    }
    cout << num * mun;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    ull num = 0;
    ull mun = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";
    for (unsigned int i = 0 ; i < v.size(); ++i)
    {
        if (v[i] == v[0]) ++num;
    }

    for (int j = v.size() - 1; j >= 0; --j)
    {
        if (v[j] == v[v.size()-1]) ++mun;
    }
    cout << num * mun;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    ull num = 0;
    ull mun = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";

    for (unsigned int i = 0 ; i < v.size(); ++i)
    {
        if (v[i] == v[0]) ++num;
        else break;
    }

    for (unsigned int j = v.size() - 1; j > 0; --j)
    {
        if (v[j] == v[v.size()-1]) ++mun;
        else break;
    }
    ull sum = v.size() / 2;
    if (v[0] == v[v.size()-1]) cout << sum*(v.size() - 1);
        else cout << num * mun;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int gcd(int a, int b)
{
    if (b == 0) return a;
    return gcd(b, a%b);
}

int main()
{
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    int num = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";
    for (uint i = 0, j = v.size()-1; i < v.size()/2; ++i, --j)
    {
        if (v[j] - v[i] == max) ++num;
    }
    cout << num * num;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    ull num = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";
    for (uint i = 0, j = v.size()-1; i < v.size()/2; ++i, --j)
    {
        if (v[j] - v[i] == max) ++num;
    }
    cout << num * num;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    ull num = 0;
    ull mun = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";

    for (unsigned int i = 0 ; i < v.size(); ++i)
    {
        if (v[i] == v[0]) ++num;
        else break;
    }

    for (unsigned int j = v.size() - 1; j > 0; --j)
    {
        if (v[j] == v[v.size()-1]) ++mun;
        else break;
    }

    if (v[0] == v[v.size()-1]) cout << v.size()/2 * (v.size() - 1);
        else cout << num * mun;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    ull num = 0;
    ull mun = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";

    for (unsigned int i = 0 ; i < v.size(); ++i)
    {
        if (v[i] == v[0]) ++num;
        else break;
    }

    for (unsigned int j = v.size() - 1; j > 0; --j)
    {
        if (v[j] == v[v.size()-1]) ++mun;
        else break;
    }
    ull sum = 0;
    if ((v.size() - 1) % 2 == 0) {
        sum = (v.size() - 1) / 2;
        sum *= v.size();
    }
    else
    {
        sum = v.size() / 2;
        sum *= v.size() - 1;
    }
    if (v[0] == v[v.size()-1]) cout << sum;
        else cout << num * mun;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    int num = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";
    for (unsigned int i = 0, j = v.size()-1; i < v.size()/2; ++i, --j)
    {
        if (v[j] - v[i] == max) ++num;
    }
    cout << num * num;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    ull num = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";
    for (unsigned int i = 0, j = v.size()-1; i < v.size()/2; ++i, --j)
    {
        if (v[j] - v[i] == max) ++num;
    }
    cout << num * num;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    ull num = 0;
    ull mun = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";

    for (unsigned int i = 0 ; i < v.size(); ++i)
    {
        if (v[i] == v[0]) ++num;
        else break;
    }

    for (unsigned int j = v.size() - 1; j > 0; --j)
    {
        if (v[j] == v[v.size()-1]) ++mun;
        else break;
    }
    ull sum = (v.size() - 1) / 2;
    if (v[0] == v[v.size()-1]) cout << sum * v.size();
        else cout << num * mun;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    int num = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";
    for (uint i = 0, j = v.size()-1; i < v.size()/2; ++i, --j)
    {
        if (v[j] - v[i] == max) ++num;
    }
    cout << num * num;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define V(n) vector<int> v(n)
#define cinN int n; cin >> n
#define cinA int a; cin >> a
#define cinB int b; cin >> b
#define cinM int m; cin >> m
#define ull unsigned long long
#define print(v, n) for(int i = 0; i < n; ++i) { cout << v[i] << " "; }
using namespace std;

int main()
{_
    cinN;
    vector<int> v(n);
    for(int i = 0; i < n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());
    ull num = 0;
    ull mun = 0;
    int max = v[v.size()-1] - v[0];
    cout << max << " ";

    for (unsigned int i = 0 ; i < v.size(); ++i)
    {
        if (v[i] == v[0]) ++num;
        else break;
    }

    for (unsigned int j = v.size() - 1; j > 0; --j)
    {
        if (v[j] == v[v.size()-1]) ++mun;
        else break;
    }
    ull sum = v.size() *(v.size() - 1);
    if (v[0] == v[v.size()-1]) cout << sum / 2;
        else cout << num * mun;
}


//~In The Name Of Allah~//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <sstream>
#include <cstring>
#include <fstream>
#include <functional>
#include <cstdio>
#include <stack>
#include <utility>
#include <set>
#include <list>
#include <queue>
#include <bitset>
using namespace std;
#define sorta(STL) sort(STL.begin(), STL.end())
#define sortb(STL) sort(STL.begin(), STL.end(), g<ll>())
#define all(STL) STL.begin(), STL.end()
#define ssum(Length) ull(Length*(Length+1)/2)
#define np(Begin, End) next_permutation(Begin, End)
#define cinp(number) cin>>number.first>>number.second
#define coutp(number) cout<<number.first<<" "<<number.second<<endl
#define sp(number) setprecision(number)<<fixed
#define getdis(x1, y1, x2, y2) double(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
#define rep(i, j) for (ll i = 0; i < j; i++)
#define rrep(i, size) for (ll i = size - 1; i > -1; i--)
#define Rep(i, j, k) for (ll i = j; i < k; i++)
#define RRep(i, j, size) for (ll i = size - 1; i >= j; i--)
#define mrep(def, mapa) for (def::iterator it = mapa.begin(); it != mapa.end(); it++)
#define mem(Array, Val) memset(Array, Val, sizeof Array)
#define clr(Stl, Val) fill(all(Stl), Val)
#define gt(Str) getline(cin, Str)
#define mk make_pair
#define g greater
#define cg cin.get()
#define go continue
#define pi 3.1415926535897932384626433832795
#define mod 1000000007
#define oo (ll) 1e9
#define ex 2.7182818284
typedef stringstream ss;
typedef string s;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<vector<int> > vii;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<pii> vpii;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<ll, ll> mii;
typedef set<int> si;
typedef multimap<int, int> mlii;
typedef map<char, ll> mci;
typedef map<string, ll> msi;
typedef map<string, string> mss;
int main(){
    //freopen("input.cpp", "r", stdin);
    //freopen("output.cpp", "w", stdout);
    ll size;
    cin >> size;
    vll array(size);
    mii base;
    rep(i, size) scanf("%I64d", &array[i]), base[array[i]]++;
    sorta(array);
    if (array[0] != array[size - 1]) printf("%I64d %I64d\n", array[size - 1] - array[0], base[array[0]] * base[array[size - 1]]);
    else printf("%I64d %I64d\n", array[size - 1] - array[0], size*(size - 1) / 2);
    return 0;
}


//~In The Name Of Allah~//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <sstream>
#include <cstring>
#include <fstream>
#include <functional>
#include <cstdio>
#include <stack>
#include <utility>
#include <set>
#include <list>
#include <queue>
#include <bitset>
using namespace std;
#define sorta(STL) sort(STL.begin(), STL.end())
#define sortp(STL) sort(STL.begin(), STL.end(), g<ll>())
#define all(STL) STL.begin(), STL.end()
#define mk make_pair
#define ssum(Length) ull(Length*(Length+1)/2)
#define np(Begin, End) next_permutation(Begin, End)
#define gt(Str) getline(cin, Str)
#define g greater
#define re reverse
#define cg cin.get()
#define go continue
#define cinp(number) cin>>number.first>>number.second
#define coutp(number) cout<<number.first<<" "<<number.second<<endl;
#define sp(number) setprecision(number)<<fixed
#define getdis(x1, y1, x2, y2) double(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
#define rep(i, j) for (ll i = 0; i < j; i++)
#define rrep(i, size) for (ll i = size - 1; i > -1; i--)
#define Rep(i, j, k) for (ll i = j; i < k; i++)
#define RRep(i, j, size) for (ll i = size - 1; i >= j; i--)
#define mrep(def, mapa) for (def::iterator it = mapa.begin(); it != mapa.end(); it++)
#define pi 3.1415926535897932384626433832795
#define mod 1000000007
#define oo (ll) 1e9
#define ex 2.7182818284
typedef stringstream ss;
typedef string s;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector< vector<int> > vii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef multimap<int, int> mlii;
typedef map<char, int> mci;
typedef map<string, int> msi;
typedef map<string, string> mss;
int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll size;
    cin >> size;
    vll array(size);
    mll base;
    rep(i, size) scanf("%ld", &array[i]), base[array[i]]++;
    sorta(array), printf("%ld%c", array[size - 1] - array[0], " ");
    if (array[0] == array[size - 1]) cout << size*(size - 1) / 2 << endl;
    else printf("%ld\n", base[array[0]] * base[array[size - 1]]);
    return 0;
}


//~In The Name Of Allah~//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <sstream>
#include <cstring>
#include <fstream>
#include <functional>
#include <cstdio>
#include <stack>
#include <utility>
#include <set>
#include <list>
#include <queue>
#include <bitset>
using namespace std;
#define sorta(STL) sort(STL.begin(), STL.end())
#define sortp(STL) sort(STL.begin(), STL.end(), g<ll>())
#define all(STL) STL.begin(), STL.end()
#define mk make_pair
#define ssum(Length) ull(Length*(Length+1)/2)
#define np(Begin, End) next_permutation(Begin, End)
#define gt(Str) getline(cin, Str)
#define g greater
#define re reverse
#define cg cin.get()
#define go continue
#define cinp(number) cin>>number.first>>number.second
#define coutp(number) cout<<number.first<<" "<<number.second<<endl;
#define sp(number) setprecision(number)<<fixed
#define getdis(x1, y1, x2, y2) double(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
#define rep(i, j) for (ll i = 0; i < j; i++)
#define rrep(i, size) for (ll i = size - 1; i > -1; i--)
#define Rep(i, j, k) for (ll i = j; i < k; i++)
#define RRep(i, j, size) for (ll i = size - 1; i >= j; i--)
#define mrep(def, mapa) for (def::iterator it = mapa.begin(); it != mapa.end(); it++)
#define pi 3.1415926535897932384626433832795
#define mod 1000000007
#define oo (ll) 1e9
#define ex 2.7182818284
typedef stringstream ss;
typedef string s;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector< vector<int> > vii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<int, int> mii;
typedef multimap<int, int> mlii;
typedef map<char, int> mci;
typedef map<string, int> msi;
typedef map<string, string> mss;
typedef map<ll, ll> mlll;
int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll size, res1, res2 = 0;
    cin >> size;
    vll array(size);
    mlll base;
    rep(i, size) cin >> array[i], base[array[i]]++;
    sorta(array), res1 = array[size - 1] - array[0];
    if (array[0] == array[size - 1]){
        cout << res1 << " " << size*(size - 1) / 2 << endl;
        return 0;
    }
    mrep(mlll, base)
    for (mlll::iterator itt = it; itt != base.end(); itt++)
    if (it != itt && abs(it->first - itt->first) == res1){
        res2 += it->second* itt->second, it->second = itt->second = 0;
    }
    cout << res1 << " " << res2 << endl;
    return 0;
}


//~In The Name Of Allah~//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <sstream>
#include <cstring>
#include <fstream>
#include <functional>
#include <cstdio>
#include <stack>
#include <utility>
#include <set>
#include <list>
#include <queue>
#include <bitset>
using namespace std;
#define sorta(STL) sort(STL.begin(), STL.end())
#define sortb(STL) sort(STL.begin(), STL.end(), g<ll>())
#define all(STL) STL.begin(), STL.end()
#define ssum(Length) ull(Length*(Length+1)/2)
#define np(Begin, End) next_permutation(Begin, End)
#define cinp(number) cin>>number.first>>number.second
#define coutp(number) cout<<number.first<<" "<<number.second<<endl
#define sp(number) setprecision(number)<<fixed
#define getdis(x1, y1, x2, y2) double(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
#define rep(i, j) for (ll i = 0; i < j; i++)
#define rrep(i, size) for (ll i = size - 1; i > -1; i--)
#define Rep(i, j, k) for (ll i = j; i < k; i++)
#define RRep(i, j, size) for (ll i = size - 1; i >= j; i--)
#define mrep(def, mapa) for (def::iterator it = mapa.begin(); it != mapa.end(); it++)
#define mem(Array, Val) memset(Array, Val, sizeof Array)
#define clr(Stl, Val) fill(all(Stl), Val)
#define gt(Str) getline(cin, Str)
#define mk make_pair
#define g greater
#define cg cin.get()
#define go continue
#define pi 3.1415926535897932384626433832795
#define mod 1000000007
#define oo (ll) 1e9
#define ex 2.7182818284
typedef stringstream ss;
typedef string s;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<vector<int> > vii;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<pii> vpii;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<ll, ll> mii;
typedef set<int> si;
typedef multimap<int, int> mlii;
typedef map<char, ll> mci;
typedef map<string, ll> msi;
typedef map<string, string> mss;
int main(){
    //freopen("input.cpp", "r", stdin);
    //freopen("output.cpp", "w", stdout);
    int size;
    cin >> size;
    vi array(size);
    mii base;
    rep(i, size) scanf("%d", &array[i]), base[array[i]]++;
    sorta(array), printf("%ld %ld\n", array[size - 1] - array[0], base[array[0]] * base[array[size - 1]]);
    return 0;
}


//~In The Name Of Allah~//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <sstream>
#include <cstring>
#include <fstream>
#include <functional>
#include <cstdio>
#include <stack>
#include <utility>
#include <set>
#include <list>
#include <queue>
#include <bitset>
using namespace std;
#define sorta(STL) sort(STL.begin(), STL.end())
#define sortp(STL) sort(STL.begin(), STL.end(), g<ll>())
#define all(STL) STL.begin(), STL.end()
#define mk make_pair
#define ssum(Length) ull(Length*(Length+1)/2)
#define np(Begin, End) next_permutation(Begin, End)
#define gt(Str) getline(cin, Str)
#define g greater
#define re reverse
#define cg cin.get()
#define go continue
#define cinp(number) cin>>number.first>>number.second
#define coutp(number) cout<<number.first<<" "<<number.second<<endl;
#define sp(number) setprecision(number)<<fixed
#define getdis(x1, y1, x2, y2) double(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
#define rep(i, j) for (ll i = 0; i < j; i++)
#define rrep(i, size) for (ll i = size - 1; i > -1; i--)
#define Rep(i, j, k) for (ll i = j; i < k; i++)
#define RRep(i, j, size) for (ll i = size - 1; i >= j; i--)
#define mrep(def, mapa) for (def::iterator it = mapa.begin(); it != mapa.end(); it++)
#define pi 3.1415926535897932384626433832795
#define mod 1000000007
#define oo (ll) 1e9
#define ex 2.7182818284
typedef stringstream ss;
typedef string s;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector< vector<int> > vii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef multimap<int, int> mlii;
typedef map<char, int> mci;
typedef map<string, int> msi;
typedef map<string, string> mss;
int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll size;
    cin >> size;
    vll array(size);
    mll base;
    rep(i, size) scanf("%ld", &array[i]), base[array[i]]++;
    sorta(array), printf("%ld%c", array[size - 1] - array[0], ' ');
    if (array[0] == array[size - 1]) cout << size*(size - 1) / 2 << endl;
    else printf("%ld\n", base[array[0]] * base[array[size - 1]]);
    return 0;
}


//~In The Name Of Allah~//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <sstream>
#include <cstring>
#include <fstream>
#include <functional>
#include <cstdio>
#include <stack>
#include <utility>
#include <set>
#include <list>
#include <queue>
#include <bitset>
using namespace std;
#define sorta(STL) sort(STL.begin(), STL.end())
#define sortp(STL) sort(STL.begin(), STL.end(), g<ll>())
#define all(STL) STL.begin(), STL.end()
#define mk make_pair
#define ssum(Length) ull(Length*(Length+1)/2)
#define np(Begin, End) next_permutation(Begin, End)
#define gt(Str) getline(cin, Str)
#define g greater
#define re reverse
#define cg cin.get()
#define go continue
#define cinp(number) cin>>number.first>>number.second
#define coutp(number) cout<<number.first<<" "<<number.second<<endl;
#define sp(number) setprecision(number)<<fixed
#define getdis(x1, y1, x2, y2) double(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
#define rep(i, j) for (ll i = 0; i < j; i++)
#define rrep(i, size) for (ll i = size - 1; i > -1; i--)
#define Rep(i, j, k) for (ll i = j; i < k; i++)
#define RRep(i, j, size) for (ll i = size - 1; i >= j; i--)
#define mrep(def, mapa) for (def::iterator it = mapa.begin(); it != mapa.end(); it++)
#define pi 3.1415926535897932384626433832795
#define mod 1000000007
#define oo (ll) 1e9
#define ex 2.7182818284
typedef stringstream ss;
typedef string s;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector< vector<int> > vii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<int, int> mii;
typedef multimap<int, int> mlii;
typedef map<char, int> mci;
typedef map<string, int> msi;
typedef map<string, string> mss;
typedef map<ll, ll> mlll;
int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll size, mini = 1000000009, maxi = 0, res1, res2 = 0;
    cin >> size;
    vll array(size);
    mlll base;
    rep(i, size){
        cin >> array[i], base[array[i]]++;
        if (maxi < array[i]) maxi = array[i];
        if (mini>array[i]) mini = array[i];
    }
    res1 = maxi - mini;
    if (!res1){
        mrep(mlll, base) res2 += it->second - 1;/////////////////////////////
        cout << res1 << " " << res2 << endl;
        return 0;
    }
    mrep(mlll, base)
    for (mlll::iterator itt = it; itt != base.end(); itt++)
    if (it != itt && abs(it->first - itt->first) == res1){
        res2 += it->second* itt->second, it->second = itt->second = 0;
    }
    cout << res1 << " " << res2 << endl;
    return 0;
}


//~In The Name Of Allah~//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <sstream>
#include <cstring>
#include <fstream>
#include <functional>
#include <cstdio>
#include <stack>
#include <utility>
#include <set>
#include <list>
#include <queue>
#include <bitset>
using namespace std;

#define all(S) S.begin(), S.end()
#define sortb(S) sort(all(S), greater<ll>())
#define sorta(S) sort(all(S))
#define cinp(N) cin>>N.first>>N.second
#define coutp(N) cout<<N.first<<" "<<N.second<<endl
#define mem(C, V) memset(C, V, sizeof C)
#define clr(S, V) fill(all(S), V)
#define ssum(N) (ll)N*(N+1)/2
#define gt(S) getline(cin, S)
#define sp(N) setprecision(N)<<fixed
#define siz(S) S.size()
#define mk make_pair
#define go continue
#define rep(i, j) for (ll i = 0; i < j; i++)
#define rrep(i, j) for (ll i = j - 1; i > -1; i--)
#define getdis(x1, y1, x2, y2) double(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
#define Rep(i, j, k) for (ll i = j; i < k; i++)
#define srep(C) for (decltype(C.begin()) it = C.begin(); it != C.end(); it++)
#define pi 3.1415926535897932384626433832795
#define ex 2.7182818284
#define mod 1000000007
#define oo (ll) 1e9

typedef stringstream  ss;
typedef string            s;
typedef long long       ll;
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<vi>      vii;
typedef vector<pii>     vpii;
typedef vector<ll>       vll;
typedef vector<bool>  vb;
typedef vector<s>       vs;
typedef map<ll, ll>      mii;
typedef map<char, ll> mci;
typedef map<s, ll>      msi;

int base[(int)2e5 + 1] = { 0 };
int main(){
    //freopen("input.cpp", "r", stdin);
    //freopen("output.cpp", "w", stdout);
    int size;
    cin >> size;
    vi array(size);
    rep(i, size) scanf("%d", &array[i]), base[array[i]]++;
    sorta(array);
    cout << min(base[array[size - 1]], base[array[0]]) << " " << (ll)base[array[size - 1]] * base[array[0]] << endl;
    return 0;
}


//~In The Name Of Allah~//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <sstream>
#include <cstring>
#include <fstream>
#include <functional>
#include <cstdio>
#include <stack>
#include <utility>
#include <set>
#include <list>
#include <queue>
#include <bitset>
using namespace std;
#define sorta(STL) sort(STL.begin(), STL.end())
#define sortp(STL) sort(STL.begin(), STL.end(), g<ll>())
#define all(STL) STL.begin(), STL.end()
#define mk make_pair
#define ssum(Length) ull(Length*(Length+1)/2)
#define np(Begin, End) next_permutation(Begin, End)
#define gt(Str) getline(cin, Str)
#define g greater
#define re reverse
#define cg cin.get()
#define go continue
#define cinp(number) cin>>number.first>>number.second
#define coutp(number) cout<<number.first<<" "<<number.second<<endl;
#define sp(number) setprecision(number)<<fixed
#define getdis(x1, y1, x2, y2) double(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
#define rep(i, j) for (ll i = 0; i < j; i++)
#define rrep(i, size) for (ll i = size - 1; i > -1; i--)
#define Rep(i, j, k) for (ll i = j; i < k; i++)
#define RRep(i, j, size) for (ll i = size - 1; i >= j; i--)
#define mrep(def, mapa) for (def::iterator it = mapa.begin(); it != mapa.end(); it++)
#define pi 3.1415926535897932384626433832795
#define mod 1000000007
#define oo (ll) 1e9
#define ex 2.7182818284
typedef stringstream ss;
typedef string s;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector< vector<int> > vii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef multimap<int, int> mlii;
typedef map<char, int> mci;
typedef map<string, int> msi;
typedef map<string, string> mss;
int main(){
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	ll size;
	cin >> size;
	vll array(size);
	mll base;
	rep(i, size) cin >> array[i], base[array[i]]++;
	sorta(array), cout << array[size - 1] - array[0] << " ";
	if (array[0] == array[size - 1]) cout << size*(size - 1) / 2 << endl;
	else cout << base[array[0]] * base[array[size - 1]] << endl;
	return 0;
}


//~In The Name Of Allah~//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <sstream>
#include <cstring>
#include <fstream>
#include <functional>
#include <cstdio>
#include <stack>
#include <utility>
#include <set>
#include <list>
#include <queue>
#include <bitset>
using namespace std;
#define sorta(STL) sort(STL.begin(), STL.end())
#define sortb(STL) sort(STL.begin(), STL.end(), g<ll>())
#define all(STL) STL.begin(), STL.end()
#define ssum(Length) ull(Length*(Length+1)/2)
#define np(Begin, End) next_permutation(Begin, End)
#define cinp(number) cin>>number.first>>number.second
#define coutp(number) cout<<number.first<<" "<<number.second<<endl
#define sp(number) setprecision(number)<<fixed
#define getdis(x1, y1, x2, y2) double(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
#define rep(i, j) for (ll i = 0; i < j; i++)
#define rrep(i, size) for (ll i = size - 1; i > -1; i--)
#define Rep(i, j, k) for (ll i = j; i < k; i++)
#define RRep(i, j, size) for (ll i = size - 1; i >= j; i--)
#define mrep(def, mapa) for (def::iterator it = mapa.begin(); it != mapa.end(); it++)
#define mem(Array, Val) memset(Array, Val, sizeof Array)
#define clr(Stl, Val) fill(all(Stl), Val)
#define gt(Str) getline(cin, Str)
#define mk make_pair
#define g greater
#define cg cin.get()
#define go continue
#define pi 3.1415926535897932384626433832795
#define mod 1000000007
#define oo (ll) 1e9
#define ex 2.7182818284
typedef stringstream ss;
typedef string s;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<vector<int> > vii;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<pii> vpii;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<ll, ll> mii;
typedef set<int> si;
typedef multimap<int, int> mlii;
typedef map<char, ll> mci;
typedef map<string, ll> msi;
typedef map<string, string> mss;
int main(){
    //freopen("input.cpp", "r", stdin);
    //freopen("output.cpp", "w", stdout);
    int size;
    cin >> size;
    vll array(size);
    mii base;
    rep(i, size) scanf("%I64d", &array[i]), base[array[i]]++;
    sorta(array), printf("%I64d %I64d\n", array[size - 1] - array[0], base[array[0]] * base[array[size - 1]]);
    return 0;
}


//~In The Name Of Allah~//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <sstream>
#include <cstring>
#include <fstream>
#include <functional>
#include <cstdio>
#include <stack>
#include <utility>
#include <set>
#include <list>
#include <queue>
#include <bitset>
using namespace std;
#define sorta(STL) sort(STL.begin(), STL.end())
#define sortp(STL) sort(STL.begin(), STL.end(), g<ll>())
#define all(STL) STL.begin(), STL.end()
#define mk make_pair
#define ssum(Length) ull(Length*(Length+1)/2)
#define np(Begin, End) next_permutation(Begin, End)
#define gt(Str) getline(cin, Str)
#define g greater
#define re reverse
#define cg cin.get()
#define go continue
#define cinp(number) cin>>number.first>>number.second
#define coutp(number) cout<<number.first<<" "<<number.second<<endl;
#define sp(number) setprecision(number)<<fixed
#define getdis(x1, y1, x2, y2) double(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
#define rep(i, j) for (ll i = 0; i < j; i++)
#define rrep(i, size) for (ll i = size - 1; i > -1; i--)
#define Rep(i, j, k) for (ll i = j; i < k; i++)
#define RRep(i, j, size) for (ll i = size - 1; i >= j; i--)
#define mrep(def, mapa) for (def::iterator it = mapa.begin(); it != mapa.end(); it++)
#define pi 3.1415926535897932384626433832795
#define mod 1000000007
#define oo (ll) 1e9
#define ex 2.7182818284
typedef stringstream ss;
typedef string s;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector< vector<int> > vii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<int, int> mii;
typedef multimap<int, int> mlii;
typedef map<char, int> mci;
typedef map<string, int> msi;
typedef map<string, string> mss;
typedef map<ll, ll> mlll;
int main(){
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	ll size, res1, res2 = 0;
	cin >> size;
	vll array(size);
	mlll base;
	rep(i, size) cin >> array[i], base[array[i]]++;
	sorta(array), res1 = array[size - 1] - array[0];
	if (array[0]==array[size-1]){
		cout << size*(size - 1) / 2 << endl;
		return 0;
	}
	mrep(mlll, base)
	for (mlll::iterator itt = it; itt != base.end(); itt++)
	if (it != itt && abs(it->first - itt->first) == res1){
		res2 += it->second* itt->second, it->second = itt->second = 0;
	}
	cout << res1 << " " << res2 << endl;
	return 0;
}