#define I do solo
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;

#define ll long long
#define ii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define f(i,a,b) for (int i=a;i<=b;i++)
const ll mod=998244353;

int n,a[505];
vector<int> v[505][505];

void fuck(int l,int r) {
    if (r-l==0) {
        v[l][r].pb(a[l]);
        return;
    }
    vector<int> ans;
    for (int i=l;i<r;i++) {
        vector<int> L,R;
        L=v[l][i];
        R=v[i+1][r];
        while (L[L.size()-1]==R[0]) {
            R[0]++;
            L.pop_back();
        }
        if (R.size()+L.size()>=ans.size()) continue;
        for (int i=0;i<R.size();i++) {
            L.pb(R[i]);
        }
        ans=L;
    }
    v[l][r]=ans;
}

void bokuwaasoloplayer() {
    scanf("%d", &n);
    f(i,1,n) scanf("%d", &a[i]);
    f(i,0,n) {
        f(j,1,n) {
            if (i+j>n) break;
            fuck(j,i+j);
        }
    }
    printf("%d", v[1][n].size());
    // cout<<v[1][n].size();
    // cout<<"\n";
    // for (int i=0;i<v[1][n].size();i++) {
    //     cout<<v[1][n][i]<<" ";
    // }
}

signed main()
{
    bokuwaasoloplayer();
    return 0;
}


#define I do solo
#include"bits/stdc++.h"
using namespace std;

#define ll long long
#define f(i,a,b) for (int i=a;i<=b;i++)
#define pb push_back
#define fi first
#define se second

#define PI 3.14159265
const long long mod=1000000007;

int n,a[1005],f[505][505],ans[505];

void inp() {
	scanf("%d", &n);
	for (int i=1;i<=n;i++) {
		scanf("%d", &a[i]);
		ans[i]=1e9;
	}
}

void bokuwaasoloplayer() {
	inp();
	for (int i=0;i<n;i++) {
		for (int j=1;j+i<=n;j++) {
			if (i==0) f[j][j]=a[j];
			else {
				f[j][j+i]=-1;
				for (int k=j;k<j+i;k++) {
					if (f[j][k]==f[k+1][j+i]&&f[j][k]!=-1) {
						f[j][j+i]=f[j][k]+1;
						break;
					}
				}
			}
		}
	}
//	for (int i=1;i<=n;i++) {
//		f(j,1,n) {
//			cout<<f[i][j]<<" ";
//		}
//		cout<<"\n";
//	}
	ans[0]=0;
	f(i,0,n-1) {
		f(j,i+1,n) {
			if (f[i+1][j]!=-1) {
				ans[j]=min(ans[j],ans[i]+1);
			}
		}
	}
//	f(i,1,n) cout<<ans[i]<<" ";
//	cout<<"\n";
	printf("%d", ans[n]);
}

signed main() {
	bokuwaasoloplayer();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'

typedef long long ll;
typedef pair<int,int> pii;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ost; // set
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset; // multiset
*/

// Bitmask

/*
bool check(int mask,int pos){return mask&(1<<pos);}
int SET(int mask,int pos){return mask|(1<<pos);}
int FLIP(int mask,int pos){return mask^(1<<pos);}
*/

int val[505][505];
int dp[505][505];

int main()
{
    int n,a[505];
    cin>>n;
    memset(dp,31,sizeof dp);
    for(int i=1;i<=n;i++){
        cin>>a[i];
        val[i][i]=a[i];
        dp[i][i]=1;
    }

    for(int len=2;len<=n;len++){
        for(int l=1;l+len-1<=n;l++){
            int r=l+len-1;
            for(int mid=l;mid<r;mid++){
                if(val[l][mid] && val[l][mid]==val[mid+1][r]){
                    val[l][r]=val[l][mid]+1;
                    dp[l][r]=1;
                }
            }
        }
    }

    dp[1][n]=n;

    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            for(int k=j;k<i;k++){
                dp[j][i]=min(dp[j][i],dp[j][k]+dp[k+1][i]);
            }
        }
    }

    cout<<dp[1][n]<<endl;

}


#include<bits/stdc++.h>
using namespace std;

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'

typedef long long ll;
typedef pair<int,int> pii;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ost; // set
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset; // multiset
*/

// Bitmask

/*
bool check(int mask,int pos){return mask&(1<<pos);}
int SET(int mask,int pos){return mask|(1<<pos);}
int FLIP(int mask,int pos){return mask^(1<<pos);}
*/

int val[505][505];
int dp[505][505];

int main()
{
    int n,a[505];
    cin>>n;
    memset(dp,31,sizeof dp);
    for(int i=1;i<=n;i++){
        cin>>a[i];
        val[i][i]=a[i];
        dp[i][i]=1;
    }

    for(int len=2;len<=n;len++){
        for(int l=1;l+len-1<=n;l++){
            int r=l+len-1;
            for(int mid=l;mid<r;mid++){
                if(val[l][mid] && val[l][mid]==val[mid+1][r]){
                    val[l][r]=val[l][mid]+1;
                    dp[l][r]=1;
                }
            }
        }
    }

    dp[1][n]=min(dp[1][n],n);

    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            for(int k=j;k<i;k++){
                dp[j][i]=min(dp[j][i],dp[j][k]+dp[k+1][i]);
            }
        }
    }

    cout<<dp[1][n]<<endl;

}


#include<bits/stdc++.h>
using namespace std;

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'

typedef long long ll;
typedef pair<int,int> pii;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ost; // set
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset; // multiset
*/

// Bitmask

/*
bool check(int mask,int pos){return mask&(1<<pos);}
int SET(int mask,int pos){return mask|(1<<pos);}
int FLIP(int mask,int pos){return mask^(1<<pos);}
*/

int val[505][505];
int dp[505][505];

int main()
{
    int n,a[505];
    cin>>n;
    memset(dp,31,sizeof dp);
    for(int i=1;i<=n;i++){
        cin>>a[i];
        val[i][i]=a[i];
        dp[i][i]=1;
    }

    for(int len=2;len<=n;len++){
        for(int l=1;l+len-1<=n;l++){
            int r=l+len-1;
            for(int mid=l;mid<r;mid++){
                if(val[l][mid] && val[l][mid]==val[mid+1][r]){
                    val[l][r]=val[l][mid]+1;
                    dp[l][r]=1;
                }
            }
        }
    }

    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            for(int k=j;k<i;k++){
                dp[j][i]=min(dp[j][i],dp[j][k]+dp[k+1][i]);
            }
        }
    }

    cout<<dp[1][n]<<endl;

}


#include<bits/stdc++.h>
using namespace std;

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'

typedef long long ll;
typedef pair<int,int> pii;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ost; // set
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset; // multiset
*/

// Bitmask

/*
bool check(int mask,int pos){return mask&(1<<pos);}
int SET(int mask,int pos){return mask|(1<<pos);}
int FLIP(int mask,int pos){return mask^(1<<pos);}
*/

int val[505][505];
int dp[505][505];

int main()
{
    int n,a[505];
    cin>>n;
    memset(dp,31,sizeof dp);
    for(int i=1;i<=n;i++){
        cin>>a[i];
        val[i][i]=a[i];
        dp[i][i]=1;
    }

    for(int len=2;len<=n;len++){
        for(int l=1;l+len-1<=n;l++){
            int r=l+len-1;
            for(int mid=l;mid<r;mid++){
                if(val[l][mid] && val[l][mid]==val[mid+1][r]){
                    val[l][r]=val[l][mid]+1;
                    dp[l][r]=1;
                }
            }
        }
    }

    dp[1][n]=n;

    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            for(int k=j;k<i;k++){
                if(val[j][k] && val[k+1][i]){
                    dp[j][i]=min(dp[j][i],dp[j][k]+dp[k+1][i]);
                }
            }
        }
    }

    cout<<dp[1][n]<<endl;

}


#include<bits/stdc++.h>
using namespace std;

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'

typedef long long ll;
typedef pair<int,int> pii;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ost; // set
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset; // multiset
*/

// Bitmask

/*
bool check(int mask,int pos){return mask&(1<<pos);}
int SET(int mask,int pos){return mask|(1<<pos);}
int FLIP(int mask,int pos){return mask^(1<<pos);}
*/

int val[505][505];
int dp[505][505];

int main()
{
    int n,a[505];
    cin>>n;
    memset(dp,31,sizeof dp);
    for(int i=1;i<=n;i++){
        cin>>a[i];
        val[i][i]=a[i];
        dp[i][i]=1;
    }

    for(int len=2;len<=n;len++){
        for(int l=1;l+len-1<=n;l++){
            int r=l+len-1;
            for(int mid=l;mid<r;mid++){
                if(val[l][mid] && val[l][mid]==val[mid+1][r]){
                    val[l][r]=val[l][mid]+1;
                    dp[l][r]=1;
                }else{
                    dp[l][r]=min(dp[l][r],dp[l][mid]+dp[mid+1][r]);
                }
            }
        }
    }

    cout<<dp[1][n]<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vvi vector<vi >
#define pi pair<ll,ll>
#define vvpi vector<vector<pi > >
#define vpi vector<pi >
#define vppi vector<pair<ll,pi > >
#define vs vector<string>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define fs first
#define se second
#define fast ios_base::sync_with_stdio(false)
#define cintie cin.tie(NULL)
map<pair<ll,ll>,ll> dp;
ll a[600];
ll dfs(ll l,ll r)
{
	if(l>=r)
	return 0;
	if(l+1==r)
	{
		return dp[mkp(l,r)]=a[l];
	}
	if(dp.count(mkp(l,r)))
	return dp[mkp(l,r)];
	dp[mkp(l,r)]=-1;
	for(ll mid=l+1;mid<r;mid++)
	{
		ll lf=dfs(l,mid);
		ll rg=dfs(mid,r);
		if(lf>0 and lf==rg)
		return dp[mkp(l,r)]=lf+1;
	}
	return dp[mkp(l,r)];
}
int main()
{
    fast;
    cintie;
	ll n,i,j;
	cin>>n;
	ll dp2[n+10];
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(i=0;i<=n;i++)
	{
		dp2[i]=1e9+7;
	}
	dp2[0]=0;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<=n;j++)
		{
			if(dfs(i,j)>0)
			{
				dp2[j]=min(dp2[j],dp2[i]+1);
			}
		}
	}
	cout<<dp2[n]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vvi vector<vi >
#define pi pair<ll,ll>
#define vvpi vector<vector<pi > >
#define vpi vector<pi >
#define vppi vector<pair<ll,pi > >
#define vs vector<string>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define fs first
#define se second
#define fast ios_base::sync_with_stdio(false)
#define cintie cin.tie(NULL)
int dp[555][555];
int a[600];
ll dfs(int l,int r)
{
	if(l>=r)
	return 0;
	if(l+1==r)
	{
		return dp[l][r]=a[l];
	}
	if(dp[l][r]!=0)
	return dp[l][r];
	dp[l][r]=-1;
	for(int mid=l+1;mid<r;mid++)
	{
		int lf=dfs(l,mid);
		int rg=dfs(mid,r);
		if(lf>0 and lf==rg)
		return dp[l][r]=lf+1;
	}
	return dp[l][r];
}
int main()
{
	fast;
	cintie;
	int n,i,j;
	cin>>n;
	int dp2[n+10];
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(i=0;i<=n;i++)
	{
		dp2[i]=int(1e9);
	}
	dp2[0]=0;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<=n;j++)
		{
			if(dfs(i,j)>0)
			{
				dp2[j]=min(dp2[j],dp2[i]+1);
			}
		}
	}
	cout<<dp2[n]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vvi vector<vi >
#define pi pair<ll,ll>
#define vvpi vector<vector<pi > >
#define vpi vector<pi >
#define vppi vector<pair<ll,pi > >
#define vs vector<string>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define fs first
#define se second
#define fast ios_base::sync_with_stdio(false)
#define cintie cin.tie(NULL)
map<pair<int,int>,int> dp;
int a[600];
ll dfs(int l,int r)
{
	if(l>=r)
	return 0;
	if(l+1==r)
	{
		return dp[mkp(l,r)]=a[l];
	}
	if(dp.count(mkp(l,r)))
	return dp[mkp(l,r)];
	dp[mkp(l,r)]=-1;
	for(int mid=l+1;mid<r;mid++)
	{
		int lf=dfs(l,mid);
		int rg=dfs(mid,r);
		if(lf>0 and lf==rg)
		return dp[mkp(l,r)]=lf+1;
	}
	return dp[mkp(l,r)];
}
int main()
{
	fast;
	cintie;
	int n,i,j;
	cin>>n;
	int dp2[n+10];
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(i=0;i<=n;i++)
	{
		dp2[i]=int(1e9);
	}
	dp2[0]=0;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<=n;j++)
		{
			if(dfs(i,j)>0)
			{
				dp2[j]=min(dp2[j],dp2[i]+1);
			}
		}
	}
	cout<<dp2[n]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vvi vector<vi >
#define pi pair<ll,ll>
#define vvpi vector<vector<pi > >
#define vpi vector<pi >
#define vppi vector<pair<ll,pi > >
#define vs vector<string>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define fs first
#define se second
#define fast ios_base::sync_with_stdio(false)
#define cintie cin.tie(NULL)
int dp[555][555];
int a[600];
ll dfs(int l,int r)
{
	if(l>=r)
	return 0;
	if(l+1==r)
	{
		return dp[l][r]=a[l];
	}
	if(dp[l][r]!=0)
	return dp[l][r];
	dp[l][r]=-1;
	for(int mid=l+1;mid<r;mid++)
	{
		int lf=dfs(l,mid);
		int rg=dfs(mid,r);
		if(lf>0 and lf==rg)
		return dp[l][r]=lf+1;
	}
	return dp[l][r];
}
int main()
{
	fast;
	cintie;
	int n,i,j;
	cin>>n;
	int dp2[n+10];
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(i=0;i<=n;i++)
	{
		dp2[i]=int(1e9);
	}
	dfs(0,n);
	dp2[0]=0;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<=n;j++)
		{
			if(dp[i][j]>0)
			{
				dp2[j]=min(dp2[j],dp2[i]+1);
			}
		}
	}
	cout<<dp2[n]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vvi vector<vi >
#define pi pair<ll,ll>
#define vvpi vector<vector<pi > >
#define vpi vector<pi >
#define vppi vector<pair<ll,pi > >
#define vs vector<string>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define fs first
#define se second
#define fast ios_base::sync_with_stdio(false)
#define cintie cin.tie(NULL)
map<pair<ll,ll>,ll> dp;
ll a[600];
ll dfs(ll l,ll r)
{
	if(l>=r)
	return 0;
	if(l+1==r)
	{
		return dp[mkp(l,r)]=a[l];
	}
	if(dp.count(mkp(l,r)))
	return dp[mkp(l,r)];
	dp[mkp(l,r)]=-1;
	for(ll mid=l+1;mid<r;mid++)
	{
		ll lf=dfs(l,mid);
		ll rg=dfs(mid,r);
		if(lf>0 and lf==rg)
		return dp[mkp(l,r)]=lf+1;
	}
	return dp[mkp(l,r)];
}
int main()
{
	ll n,i,j;
	cin>>n;
	ll dp2[n+10];
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(i=0;i<=n;i++)
	{
		dp2[i]=1e9+7;
	}
	dp2[0]=0;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<=n;j++)
		{
			if(dfs(i,j)>0)
			{
				dp2[j]=min(dp2[j],dp2[i]+1);
			}
		}
	}
	cout<<dp2[n]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vvi vector<vi >
#define pi pair<ll,ll>
#define vvpi vector<vector<pi > >
#define vpi vector<pi >
#define vppi vector<pair<ll,pi > >
#define vs vector<string>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define fs first
#define se second
#define fast ios_base::sync_with_stdio(false)
#define cintie cin.tie(NULL)
map<pair<int,int>,int> dp;
int a[600];
ll dfs(ll l,ll r)
{
	if(l>=r)
	return 0;
	if(l+1==r)
	{
		return dp[mkp(l,r)]=a[l];
	}
	if(dp.count(mkp(l,r)))
	return dp[mkp(l,r)];
	dp[mkp(l,r)]=-1;
	for(ll mid=l+1;mid<r;mid++)
	{
		ll lf=dfs(l,mid);
		ll rg=dfs(mid,r);
		if(lf>0 and lf==rg)
		return dp[mkp(l,r)]=lf+1;
	}
	return dp[mkp(l,r)];
}
int main()
{
	int n,i,j;
	cin>>n;
	int dp2[n+10];
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for(i=0;i<=n;i++)
	{
		dp2[i]=int(1e9);
	}
	dp2[0]=0;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<=n;j++)
		{
			if(dfs(i,j)>0)
			{
				dp2[j]=min(dp2[j],dp2[i]+1);
			}
		}
	}
	cout<<dp2[n]<<endl;
}


//Coded by tzc_wk
/*
���ݲ���գ����������ᡣ
��ⲻ���꣬���������ᡣ
�߽粻���У����������ᡣ
̰�Ĳ�֤�������������ᡣ
D P ˳������������ᡣ
��С�ٵȺţ����������ᡣ
������ͳһ�����������ᡣ
Խ�粻�жϣ����������ᡣ
���Բ�ע�ͣ����������ᡣ
����� l l�����������ᡣ
*/
#include <bits/stdc++.h>
using namespace std;
#define fi			first
#define se			second
#define fz(i,a,b)	for(int i=a;i<=b;i++)
#define fd(i,a,b)	for(int i=a;i>=b;i--)
#define foreach(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define all(a)		a.begin(),a.end()
#define giveup(...) return printf(__VA_ARGS__),0;
#define fill0(a)	memset(a,0,sizeof(a))
#define fill1(a)	memset(a,-1,sizeof(a))
#define fillbig(a)	memset(a,0x3f,sizeof(a))
#define fillsmall(a) memset(a,0xcf,sizeof(a))
#define mask(a)		(1ll<<(a))
#define maskx(a,x)	((a)<<(x))
#define _bit(a,x)	(((a)>>(x))&1)
#define _sz(a)		((int)(a).size())
#define filei(a)	freopen(a,"r",stdin);
#define fileo(a)	freopen(a,"w",stdout);
#define fileio(a) 	freopen(a".in","r",stdin);freopen(a".out","w",stdout)
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
#define put(x)		putchar(x)
#define eoln        put('\n')
#define space		put(' ')
#define y1			y_chenxiaoyan_1
#define y0			y_chenxiaoyan_0
typedef pair<int,int> pii;
inline int read(){
	int x=0,neg=1;char c=getchar();
	while(!isdigit(c)){
		if(c=='-')	neg=-1;
		c=getchar();
	}
	while(isdigit(c))	x=x*10+c-'0',c=getchar();
	return x*neg;
}
inline void print(int x){
	if(x<0){
		putchar('-');
		print(abs(x));
		return;
	}
	if(x<=9)	putchar(x+'0');
	else{
		print(x/10);
		putchar(x%10+'0');
	}
}
inline int qpow(int x,int e,int _MOD){
	int ans=1;
	while(e){
		if(e&1)	ans=ans*x%_MOD;
		x=x*x%_MOD;
		e>>=1;
	}
	return ans;
}
int n=read(),a[505],dp1[505][1005],dp2[505][505];
inline int dfs(int l,int r){
	if(l>r)			return 0;
	if(l==r)		return 1;
	if(~dp2[l][r])	return dp2[l][r];
	dp2[l][r]=0x3f3f3f3f;
	fz(i,1,1000)	dp2[l][r]=min(dp2[l][r],1+dfs(dp1[l][i],r));
	fz(i,l,r-1)		dp2[l][r]=min(dp2[l][r],dfs(l,i)+dfs(i+1,r));
	return dp2[l][r];
}
signed main(){
	fz(i,1,n)	a[i]=read(),dp1[i][a[i]]=i+1;
	fz(t,1,1000){
		fz(i,1,n)
			if(dp1[i][t]==0)
				dp1[i][t]=dp1[dp1[i][t-1]][t-1];
	}
	fill1(dp2);
	cout<<dfs(1,n)<<endl;
	return 0;
}


//Coded by tzc_wk
/*
���ݲ���գ����������ᡣ
��ⲻ���꣬���������ᡣ
�߽粻���У����������ᡣ
̰�Ĳ�֤�������������ᡣ
D P ˳������������ᡣ
��С�ٵȺţ����������ᡣ
������ͳһ�����������ᡣ
Խ�粻�жϣ����������ᡣ
���Բ�ע�ͣ����������ᡣ
����� l l�����������ᡣ
*/
#include <bits/stdc++.h>
using namespace std;
#define fi			first
#define se			second
#define fz(i,a,b)	for(int i=a;i<=b;i++)
#define fd(i,a,b)	for(int i=a;i>=b;i--)
#define foreach(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define all(a)		a.begin(),a.end()
#define giveup(...) return printf(__VA_ARGS__),0;
#define fill0(a)	memset(a,0,sizeof(a))
#define fill1(a)	memset(a,-1,sizeof(a))
#define fillbig(a)	memset(a,0x3f,sizeof(a))
#define fillsmall(a) memset(a,0xcf,sizeof(a))
#define mask(a)		(1ll<<(a))
#define maskx(a,x)	((a)<<(x))
#define _bit(a,x)	(((a)>>(x))&1)
#define _sz(a)		((int)(a).size())
#define filei(a)	freopen(a,"r",stdin);
#define fileo(a)	freopen(a,"w",stdout);
#define fileio(a) 	freopen(a".in","r",stdin);freopen(a".out","w",stdout)
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
#define put(x)		putchar(x)
#define eoln        put('\n')
#define space		put(' ')
#define y1			y_chenxiaoyan_1
#define y0			y_chenxiaoyan_0
typedef pair<int,int> pii;
inline int read(){
	int x=0,neg=1;char c=getchar();
	while(!isdigit(c)){
		if(c=='-')	neg=-1;
		c=getchar();
	}
	while(isdigit(c))	x=x*10+c-'0',c=getchar();
	return x*neg;
}
inline void print(int x){
	if(x<0){
		putchar('-');
		print(abs(x));
		return;
	}
	if(x<=9)	putchar(x+'0');
	else{
		print(x/10);
		putchar(x%10+'0');
	}
}
inline int qpow(int x,int e,int _MOD){
	int ans=1;
	while(e){
		if(e&1)	ans=ans*x%_MOD;
		x=x*x%_MOD;
		e>>=1;
	}
	return ans;
}
int n=read(),a[505],dp[505][505];
bool is[505][505];
inline int dfs(int l,int r){
	if(l>r)			return 0;
	if(l==r)		return 1;
	if(~dp[l][r])	return dp[l][r];
	dp[l][r]=r-l+1;
	fz(i,l,r)		if(is[l][i])	dp[l][r]=min(dp[l][r],1+dfs(i+1,r));
	fz(i,l,r-1)		dp[l][r]=min(dp[l][r],dfs(l,i)+dfs(i+1,r));
	return dp[l][r];
}
int mn[505][505];
signed main(){
	fz(i,1,n)	a[i]=read(),is[i][i]=1;
	fz(i,1,n)	fz(j,1,i){
		mn[j][i]=0x3f3f3f3f;
		fz(k,j,i)	mn[j][i]=min(mn[j][i],a[k]);
	}
	fz(len,2,n){
		for(int l=1,r=len;r<=n;l++,r++){
			fz(i,l,r-1){
				if(is[l][i]&&is[i+1][r]){
					int k1=mn[l][i]+(i-l);
					int k2=mn[i+1][r]+r-(i+1);
					if(k1==k2)	is[l][r]=1;
				}
			}
		}
	}
	fill1(dp);
	cout<<dfs(1,n)<<endl;
	return 0;
}
/*
10
4 3 3 4 5 6 7 8 9 10
*/


//Coded by tzc_wk
/*
���ݲ���գ����������ᡣ
��ⲻ���꣬���������ᡣ
�߽粻���У����������ᡣ
̰�Ĳ�֤�������������ᡣ
D P ˳������������ᡣ
��С�ٵȺţ����������ᡣ
������ͳһ�����������ᡣ
Խ�粻�жϣ����������ᡣ
���Բ�ע�ͣ����������ᡣ
����� l l�����������ᡣ
*/
#include <bits/stdc++.h>
using namespace std;
#define fi			first
#define se			second
#define fz(i,a,b)	for(int i=a;i<=b;i++)
#define fd(i,a,b)	for(int i=a;i>=b;i--)
#define foreach(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define all(a)		a.begin(),a.end()
#define giveup(...) return printf(__VA_ARGS__),0;
#define fill0(a)	memset(a,0,sizeof(a))
#define fill1(a)	memset(a,-1,sizeof(a))
#define fillbig(a)	memset(a,0x3f,sizeof(a))
#define fillsmall(a) memset(a,0xcf,sizeof(a))
#define mask(a)		(1ll<<(a))
#define maskx(a,x)	((a)<<(x))
#define _bit(a,x)	(((a)>>(x))&1)
#define _sz(a)		((int)(a).size())
#define filei(a)	freopen(a,"r",stdin);
#define fileo(a)	freopen(a,"w",stdout);
#define fileio(a) 	freopen(a".in","r",stdin);freopen(a".out","w",stdout)
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
#define put(x)		putchar(x)
#define eoln        put('\n')
#define space		put(' ')
#define y1			y_chenxiaoyan_1
#define y0			y_chenxiaoyan_0
typedef pair<int,int> pii;
inline int read(){
	int x=0,neg=1;char c=getchar();
	while(!isdigit(c)){
		if(c=='-')	neg=-1;
		c=getchar();
	}
	while(isdigit(c))	x=x*10+c-'0',c=getchar();
	return x*neg;
}
inline void print(int x){
	if(x<0){
		putchar('-');
		print(abs(x));
		return;
	}
	if(x<=9)	putchar(x+'0');
	else{
		print(x/10);
		putchar(x%10+'0');
	}
}
inline int qpow(int x,int e,int _MOD){
	int ans=1;
	while(e){
		if(e&1)	ans=ans*x%_MOD;
		x=x*x%_MOD;
		e>>=1;
	}
	return ans;
}
int n=read(),a[505],dp[505][505];
int is[505][505];
inline int dfs(int l,int r){
	if(l>r)			return 0;
	if(l==r)		return 1;
	if(~dp[l][r])	return dp[l][r];
	dp[l][r]=r-l+1;
	fz(i,l,r)		if(is[l][i])	dp[l][r]=min(dp[l][r],1+dfs(i+1,r));
	fz(i,l,r-1)		dp[l][r]=min(dp[l][r],dfs(l,i)+dfs(i+1,r));
	return dp[l][r];
}
int mn[505][505];
signed main(){
	fz(i,1,n)	a[i]=read(),is[i][i]=a[i];
	fz(i,1,n)	fz(j,1,i){
		mn[j][i]=0x3f3f3f3f;
		fz(k,j,i)	mn[j][i]=min(mn[j][i],a[k]);
	}
	fz(len,2,n){
		for(int l=1,r=len;r<=n;l++,r++){
			fz(i,l,r-1){
				if(is[l][i]&&is[i+1][r]){
					if(is[l][i]==is[i+1][r])	is[l][r]=is[l][i]+1;
				}
			}
//			cout<<l<<" "<<r<<" "<<is[l][r]<<endl;
		}
	}
	fill1(dp);
	cout<<dfs(1,n)<<endl;
	return 0;
}
/*
10
4 3 3 4 5 6 7 8 9 10
*/
/*
8
4 3 1 1 1 1 4 4
*/


//Coded by tzc_wk
/*
���ݲ���գ����������ᡣ
��ⲻ���꣬���������ᡣ
�߽粻���У����������ᡣ
̰�Ĳ�֤�������������ᡣ
D P ˳������������ᡣ
��С�ٵȺţ����������ᡣ
������ͳһ�����������ᡣ
Խ�粻�жϣ����������ᡣ
���Բ�ע�ͣ����������ᡣ
����� l l�����������ᡣ
*/
#include <bits/stdc++.h>
using namespace std;
#define fi			first
#define se			second
#define fz(i,a,b)	for(int i=a;i<=b;i++)
#define fd(i,a,b)	for(int i=a;i>=b;i--)
#define foreach(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define all(a)		a.begin(),a.end()
#define giveup(...) return printf(__VA_ARGS__),0;
#define fill0(a)	memset(a,0,sizeof(a))
#define fill1(a)	memset(a,-1,sizeof(a))
#define fillbig(a)	memset(a,0x3f,sizeof(a))
#define fillsmall(a) memset(a,0xcf,sizeof(a))
#define mask(a)		(1ll<<(a))
#define maskx(a,x)	((a)<<(x))
#define _bit(a,x)	(((a)>>(x))&1)
#define _sz(a)		((int)(a).size())
#define filei(a)	freopen(a,"r",stdin);
#define fileo(a)	freopen(a,"w",stdout);
#define fileio(a) 	freopen(a".in","r",stdin);freopen(a".out","w",stdout)
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
#define put(x)		putchar(x)
#define eoln        put('\n')
#define space		put(' ')
#define y1			y_chenxiaoyan_1
#define y0			y_chenxiaoyan_0
typedef pair<int,int> pii;
inline int read(){
	int x=0,neg=1;char c=getchar();
	while(!isdigit(c)){
		if(c=='-')	neg=-1;
		c=getchar();
	}
	while(isdigit(c))	x=x*10+c-'0',c=getchar();
	return x*neg;
}
inline void print(int x){
	if(x<0){
		putchar('-');
		print(abs(x));
		return;
	}
	if(x<=9)	putchar(x+'0');
	else{
		print(x/10);
		putchar(x%10+'0');
	}
}
inline int qpow(int x,int e,int _MOD){
	int ans=1;
	while(e){
		if(e&1)	ans=ans*x%_MOD;
		x=x*x%_MOD;
		e>>=1;
	}
	return ans;
}
int n=read(),a[505],dp1[505][2005],dp2[505][505];
inline int dfs(int l,int r){
	if(l>r)			return 0;
	if(l==r)		return 1;
	if(~dp2[l][r])	return dp2[l][r];
	dp2[l][r]=0x3f3f3f3f;
	fz(i,1,2000)	dp2[l][r]=min(dp2[l][r],1+dfs(dp1[l][i],r));
	fz(i,l,r-1)		dp2[l][r]=min(dp2[l][r],dfs(l,i)+dfs(i+1,r));
	return dp2[l][r];
}
signed main(){
	fz(i,1,n)	a[i]=read(),dp1[i][a[i]]=i+1;
	fz(t,1,2000){
		fz(i,1,n)
			if(dp1[i][t]==0)
				dp1[i][t]=dp1[dp1[i][t-1]][t-1];
	}
	fill1(dp2);
	cout<<dfs(1,n)<<endl;
	return 0;
}


//Coded by tzc_wk
/*
���ݲ���գ����������ᡣ
��ⲻ���꣬���������ᡣ
�߽粻���У����������ᡣ
̰�Ĳ�֤�������������ᡣ
D P ˳������������ᡣ
��С�ٵȺţ����������ᡣ
������ͳһ�����������ᡣ
Խ�粻�жϣ����������ᡣ
���Բ�ע�ͣ����������ᡣ
����� l l�����������ᡣ
*/
#include <bits/stdc++.h>
using namespace std;
#define fi			first
#define se			second
#define fz(i,a,b)	for(int i=a;i<=b;i++)
#define fd(i,a,b)	for(int i=a;i>=b;i--)
#define foreach(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define all(a)		a.begin(),a.end()
#define giveup(...) return printf(__VA_ARGS__),0;
#define fill0(a)	memset(a,0,sizeof(a))
#define fill1(a)	memset(a,-1,sizeof(a))
#define fillbig(a)	memset(a,0x3f,sizeof(a))
#define fillsmall(a) memset(a,0xcf,sizeof(a))
#define mask(a)		(1ll<<(a))
#define maskx(a,x)	((a)<<(x))
#define _bit(a,x)	(((a)>>(x))&1)
#define _sz(a)		((int)(a).size())
#define filei(a)	freopen(a,"r",stdin);
#define fileo(a)	freopen(a,"w",stdout);
#define fileio(a) 	freopen(a".in","r",stdin);freopen(a".out","w",stdout)
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
#define put(x)		putchar(x)
#define eoln        put('\n')
#define space		put(' ')
#define y1			y_chenxiaoyan_1
#define y0			y_chenxiaoyan_0
typedef pair<int,int> pii;
inline int read(){
	int x=0,neg=1;char c=getchar();
	while(!isdigit(c)){
		if(c=='-')	neg=-1;
		c=getchar();
	}
	while(isdigit(c))	x=x*10+c-'0',c=getchar();
	return x*neg;
}
inline void print(int x){
	if(x<0){
		putchar('-');
		print(abs(x));
		return;
	}
	if(x<=9)	putchar(x+'0');
	else{
		print(x/10);
		putchar(x%10+'0');
	}
}
inline int qpow(int x,int e,int _MOD){
	int ans=1;
	while(e){
		if(e&1)	ans=ans*x%_MOD;
		x=x*x%_MOD;
		e>>=1;
	}
	return ans;
}
int n=read(),a[505],dp1[505][1005],dp2[505][505];
inline int dfs(int l,int r){
	if(l>r)			return 0;
	if(l==r)		return 1;
	if(~dp2[l][r])	return dp2[l][r];
	dp2[l][r]=0x3f3f3f3f;
	fz(i,1,2000)	dp2[l][r]=min(dp2[l][r],1+dfs(dp1[l][i],r));
	fz(i,l,r-1)		dp2[l][r]=min(dp2[l][r],dfs(l,i)+dfs(i+1,r));
	return dp2[l][r];
}
signed main(){
	fz(i,1,n)	a[i]=read(),dp1[i][a[i]]=i+1;
	fz(t,1,2000){
		fz(i,1,n)
			if(dp1[i][t]==0)
				dp1[i][t]=dp1[dp1[i][t-1]][t-1];
	}
	fill1(dp2);
	cout<<dfs(1,n)<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ins insert
#define er erase

#define ll long long
#define fi first
#define se second
#define dou long double

#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define si size()
#define allv(v) v.begin(), v.end()
#define res resize

#define deb if (debug)
#define cth deb cout << "H\n";
#define ct(i) deb cout << i << '\n';
#define ctv(v) deb{ for(auto i: v) cout << i << " "; cout << '\n';}
#define ctvv(v) deb{ for(ll i = 0; i < v.si; ++i){cout << i << ": "; for(auto j: v[i]) cout << j << " "; cout << '\n';}}

int debug = 0;

int n;
vector <int> v1;
int ans = 0;

void slv(vector <int> v, int a){
	int now = -1, d = 0, mn = (ll)1e9, ind = 0;
	for(int i = 0; i < v.si; ++i){
		if(now != v[i]){
			now = v[i];
			d = 1;
		}else
			++d;
		if(d == 2 && mn > now){
			mn = now;
			ind = i-1;
		}
	}
	if(mn == (ll)1e9){
		ans = max(ans, a);
		return;
	}
	int cnt = 0, indn = ind;
	for(int i = ind; i < v.si && v[i] == mn; ++i, ++indn)
		++cnt;
	if(cnt%2 == 0){
		vector <int> nw;
		for(int i = 0; i < ind; ++i)
			nw.pb(v[i]);
		for(int i = 0; i < cnt/2; ++i)
			nw.pb(mn+1);
		for(int i = indn; i < v.si; ++i)
			nw.pb(v[i]);
		slv(nw, a+cnt/2);
	}else{
		vector <int> nw1, nw2;
		for(int i = 0; i < ind; ++i)
			nw1.pb(v[i]);
		for(int i = 0; i < cnt/2; ++i){
			nw1.pb(mn+1);
			nw2.pb(mn+1);
		}
		for(int i = indn; i < v.si; ++i)
			nw2.pb(v[i]);
		slv(nw1, a+cnt/2);
		slv(nw2, a+cnt/2);
	}
}

int main(){
	if(!debug) ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	cin >> n;
	v1.res(n);
	for(int i = 0; i < n; ++i)
		cin >> v1[i];

	slv(v1, 0);
	cout << n-ans;

	return 0;
}
/*

*/


#include <bits/stdc++.h>

using namespace std;

#define ins insert
#define er erase

#define ll long long
#define fi first
#define se second
#define dou long double

#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define si size()
#define allv(v) v.begin(), v.end()
#define res resize

#define deb if (debug)
#define cth deb cout << "H\n";
#define ct(i) deb cout << i << '\n';
#define ctv(v) deb{ for(auto i: v) cout << i << " "; cout << '\n';}
#define ctvv(v) deb{ for(ll i = 0; i < v.si; ++i){cout << i << ": "; for(auto j: v[i]) cout << j << " "; cout << '\n';}}

int debug = 0;

int n;
vector <int> v1;

int slv(vector <int> v){
	int now = -1, d = 0, mn = (ll)1e9, ind = 0;
	for(int i = 0; i < v.si; ++i){
		if(now != v[i]){
			now = v[i];
			d = 1;
		}else
			++d;
		if(d == 2 && mn > now){
			mn = now;
			ind = i-1;
		}
	}
	if(mn == (ll)1e9)
		return 0;
	int cnt = 0, indn = ind;
	for(int i = ind; i < v.si && v[i] == mn; ++i, ++indn)
		++cnt;
	if(cnt%2 == 0){
		vector <int> nw;
		for(int i = 0; i < ind; ++i)
			nw.pb(v[i]);
		for(int i = 0; i < cnt/2; ++i)
			nw.pb(mn+1);
		for(int i = indn; i < v.si; ++i)
			nw.pb(v[i]);
		return cnt/2 + slv(nw);
	}else{
		vector <int> nw1, nw2, nw3, nw4;
		for(int i = 0; i < ind; ++i){
			nw1.pb(v[i]);
			nw3.pb(v[i]);
		}
		for(int i = 0; i < cnt/2; ++i){
			nw1.pb(mn+1);
			nw2.pb(mn+1);
		}
		for(int i = indn; i < v.si; ++i){
			nw2.pb(v[i]);
			nw4.pb(v[i]);
		}
		return cnt/2 + max(slv(nw1) + slv(nw4), slv(nw2) + slv(nw3));
	}
}

int main(){
	if(!debug) ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	cin >> n;
	v1.res(n);
	for(int i = 0; i < n; ++i)
		cin >> v1[i];

	cout << n-slv(v1);

	return 0;
}
/*

*/


#include <bits/stdc++.h>

using namespace std;

#define ins insert
#define er erase

#define ll long long
#define fi first
#define se second
#define dou long double

#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define si size()
#define allv(v) v.begin(), v.end()
#define res resize

#define deb if (debug)
#define cth deb cout << "H\n";
#define ct(i) deb cout << i << '\n';
#define ctv(v) deb{ for(auto i: v) cout << i << " "; cout << '\n';}
#define ctvv(v) deb{ for(ll i = 0; i < v.si; ++i){cout << i << ": "; for(auto j: v[i]) cout << j << " "; cout << '\n';}}

int debug = 0;

int n;
vector <int> v1;
int ans = 0;

void slv(vector <int> v, int a){
	ctv(v)
	int now = -1, d = 0, mn = (ll)1e9, ind = 0;
	for(int i = 0; i < v.si; ++i){
		if(now != v[i]){
			now = v[i];
			d = 1;
		}else
			++d;
		if(d > 1 && mn > now){
			mn = now;
			ind = i-d+1;
		}
	}
	deb cout << "///" << ind << '\n';
	if(mn == (ll)1e9){
		ans = max(ans, a);
		return;
	}
	int cnt = 0, indn = ind;
	for(int i = ind; i < v.si && v[i] == mn; ++i, ++indn)
		++cnt;
	ct(cnt)
	ct(indn)
	if(cnt%2 == 0){
		vector <int> nw;
		for(int i = 0; i < ind; ++i)
			nw.pb(v[i]);
		for(int i = 0; i < cnt/2; ++i)
			nw.pb(mn+1);
		for(int i = indn; i < v.si; ++i)
			nw.pb(v[i]);
		slv(nw, a+cnt/2);
	}else{
		vector <int> nw1, nw2;
		for(int i = 0; i < ind; ++i)
			nw1.pb(v[i]);
		for(int i = 0; i < cnt/2; ++i){
			nw1.pb(mn+1);
			nw2.pb(mn+1);
		}
		for(int i = indn; i < v.si; ++i)
			nw2.pb(v[i]);
		slv(nw1, a+cnt/2);
		slv(nw2, a+cnt/2);
	}
}

int main(){
	if(!debug) ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	cin >> n;
	v1.res(n);
	for(int i = 0; i < n; ++i)
		cin >> v1[i];

	slv(v1, 0);
	cout << n-ans;

	return 0;
}
/*

*/


#include <bits/stdc++.h>

#define ins insert
#define er erase

#define ll long long
#define fi first
#define se second
#define dou long double

#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define si size()
#define allv(v) v.begin(), v.end()
#define res resize

#define deb if (debug)
#define cth deb cout << "H\n";
#define ct(i) deb cout << i << '\n';
#define ctv(v) deb{ for(auto i: v) cout << i << " "; cout << '\n';}
#define ctvv(v) deb{ for(ll i = 0; i < v.si; ++i){cout << i << ": "; for(auto j: v[i]) cout << j << " "; cout << '\n';}}

using namespace std;

bool debug = 0;

int main(){
	if(!debug) ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	int n;
	cin >> n;
	vector <int> v(n);
	for(int i = 0; i < n; ++i)
		cin >> v[i];

	vector <vector <int>> dp(n, vector <int> (n, -1));
	for(int i = 0; i < n; ++i)
		dp[i][i] = v[i];
	for(int i = 1; i < n; ++i)
		for(int j = 0; j < n-i; ++j)
			for(int h = j; h < j+i; ++h)
				if(dp[j][h] != -1 && dp[h+1][j+i] == dp[j][h]){
					dp[j][j+i] = dp[j][h]+1;
					break;
				}

	ctvv(dp)

	vector <int> dp2(n+1, n);
	dp2[0] = 0;
	for(int i = 0; i <= n; ++i)
		for(int j = i; j < n; ++j)
			if(dp[i][j] != -1)
				dp2[j+1] = min(dp2[i] + 1, dp2[j+1]);
	ctv(dp2)
	cout << dp2[n];

	return 0;
}
/*

*/


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1+500;
const int inf = 1111;

typedef pair<int,int> II;

int n, a[maxn], dp[maxn][1111];
vector<int> re[maxn];

main()
{
	#ifndef ONLINE_JUDGE
	freopen("abc.inp","r",stdin);
	freopen("abc.out","w",stdout);
	#endif //ONLINE_JUDGE
	ios_base::sync_with_stdio(0);
	cin.tie();
	cout.tie();

	cin>>n;
	for(int i=0;i<=n;++i) for(int val=0;val<=1009;++val) dp[i][val]=-1;
	for(int i=0;i<n;++i){
    cin>>a[i];
    dp[i][a[i]]=i+1;
	}

	for(int val=1;val<=1009;++val){
    for(int i=n-1;i>=0;--i){
    	if(dp[i][val]==-1 && dp[i][val-1]!= -1)
				dp[i][val] = dp[ dp[i][val-1] ][val-1];
				re[i].push_back(dp[i][val]);
    }
	}

	vector<int> f(n + 1, inf);
	f[0]=0;

	for(int i=0;i<n;++i){
		for(auto en : re[i]){
      f[en] = min(f[en], f[i]+1);
		}
	}
	cout<<f[n];

   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1+500;
const int inf = 1111;

typedef pair<int,int> II;

int n, a[maxn], dp[maxn][1111];
vector<int> re[maxn];

main()
{
	#ifndef ONLINE_JUDGE
	freopen("abc.inp","r",stdin);
	freopen("abc.out","w",stdout);
	#endif //ONLINE_JUDGE
	ios_base::sync_with_stdio(0);
	cin.tie();
	cout.tie();

	cin>>n;
	memset(dp,0xff,sizeof dp);
	for(int i=0;i<n;++i){
    cin>>a[i];
    dp[i][a[i]]=i+1;
	}

	for(int val=1;val<=1000;++val){
    for(int i=n-1;i>=0;--i){
    	if(dp[i][val]==-1 && dp[i][val-1]!= -1)
				dp[i][val] = dp[ dp[i][val-1] ][val-1];
				re[i].push_back(dp[i][val]);
    }
	}

	vector<int> f(n + 1, inf);
	f[0]=0;

	for(int i=0;i<n;++i){
		for(auto en : re[i]){
      f[en] = min(f[en], f[i]+1);
		}
	}
	cout<<f[n];

   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1+500;
const int inf = 1111;

typedef pair<int,int> II;

int n, a[maxn], dp[maxn][1111];
vector<int> re[maxn];

main()
{
	#ifndef ONLINE_JUDGE
	freopen("abc.inp","r",stdin);
	freopen("abc.out","w",stdout);
	#endif //ONLINE_JUDGE
	ios_base::sync_with_stdio(0);
	cin.tie();
	cout.tie();

	cin>>n;
	for(int i=0;i<=n;++i) for(int val=0;val<=1000;++val) dp[i][val]=-1;
	for(int i=0;i<n;++i){
    cin>>a[i];
    dp[i][a[i]]=i+1;
	}

	for(int val=1;val<=1000;++val){
    for(int i=n-1;i>=0;--i){
    	if(dp[i][val]==-1 && dp[i][val-1]!= -1)
				dp[i][val] = dp[ dp[i][val-1] ][val-1];
				re[i].push_back(dp[i][val]);
    }
	}

	vector<int> f(n + 1, inf);
	f[0]=0;

	for(int i=0;i<n;++i){
		for(auto en : re[i]){
      f[en] = min(f[en], f[i]+1);
		}
	}
	cout<<f[n];

   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1+500;
const int inf = 1111;

typedef pair<int,int> II;

int n, a[maxn], dp[maxn][1111];
vector<int> re[maxn];

main()
{
	#ifndef ONLINE_JUDGE
	freopen("abc.inp","r",stdin);
	freopen("abc.out","w",stdout);
	#endif //ONLINE_JUDGE
	ios_base::sync_with_stdio(0);
	cin.tie();
	cout.tie();

	cin>>n;
	for(int i=0;i<n;++i) for(int val=0;val<=1000;++val) dp[i][val]=-1;
	for(int i=0;i<n;++i){
    cin>>a[i];
    dp[i][a[i]]=i+1;
	}
	if(n==500 && a[0]==30) cerr<<1;

	for(int val=1;val<=1000;++val){
    for(int i=n-1;i>=0;--i){
    	if(dp[i][val]==-1 && dp[i][val-1]!= -1)
				dp[i][val] = dp[ dp[i][val-1] ][val-1];
				re[i].push_back(dp[i][val]);
    }
	}
	if(n==500 && a[0]==30) cerr<<1;
	vector<int> f(n + 1, inf);
	f[0]=0;

	for(int i=0;i<n;++i){
		for(auto en : re[i]){
      f[en] = min(f[en], f[i]+1);
		}
	}
	if(n==500 && a[0]==30) cerr<<1;
	cout<<f[n];

   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1+500;
const int inf = 1111;

typedef pair<int,int> II;

int n, a[maxn], dp[maxn][1111];
vector<int> re[maxn];

main()
{
	#ifndef ONLINE_JUDGE
	freopen("abc.inp","r",stdin);
	freopen("abc.out","w",stdout);
	#endif //ONLINE_JUDGE
	ios_base::sync_with_stdio(0);
	cin.tie();
	cout.tie();

	cin>>n;
	for(int i=0;i<=n;++i) for(int val=0;val<=1000;++val) dp[i][val]=-1;
	for(int i=0;i<n;++i){
    cin>>a[i];
    dp[i][a[i]]=i+1;
	}

	for(int val=1;val<=1000;++val){
    for(int i=n-1;i>=0;--i){
//				if(dp[496][4]==0&&val==4) cout<<i<<'\n';
    	if(dp[i][val]==-1 && dp[i][val-1] != -1){
				dp[i][val] = dp[ dp[i][val-1] ][val-1];
				re[i].push_back(dp[i][val]);
//				if(val==4&&i==496) cout<< " "<<dp[i][val]<<'\n';
			}
    }
	}

	vector<int> f(n + 1, inf);
	f[0]=0;
//	for(auto en : re[0]) cout<<en<< " ";

	for(int i=0;i<n;++i){
		for(auto en : re[i]){
      f[en] = min(f[en], f[i]+1);
		}
	}
	cout<<f[n];

   return 0;
}


#include <bits/stdc++.h>

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    int n, m, i, j, k;
    std::cin >> n;
    std::vector < int > x(n);
    int dp[n][n];
    int st[n][n];
    int dr[n][n];
    memset (dp, 0, sizeof dp);

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

    for (i=0; i<n; i++)
        for (j=i; j<n; j++){
            dp[i][j] = j - i + 1;
            st[i][j] = x[i];
            dr[i][j] = x[j];
        }

    int len = 1;
    for (i=0; i<n-len; i++){
        j = i + len;
        if (x[i] == x[j]){
            dp[i][j] = 1;
            st[i][j] = x[i] + 1;
            dr[i][j] = x[j] + 1;
        }
        else
            dp[i][j] = 2;
    }

    for (len=2; len<n; len++){
        for (i=0; i<n-len; i++){
            j = i + len;
            for (k=i; k<j; k++){
                int ok = 0, update;
                if (dr[i][k] == st[k+1][j])
                    ok = 1;
                update = dp[i][k] + dp[k+1][j] - ok;
                if (dp[i][j] > update){
                    if (dp[i][k] == 1)
                        st[i][j] = st[i][k] + 1;
                    else
                        st[i][j] = st[i][k];

                    if (dp[k+1][j] == 1)
                        dr[i][j] = dr[k+1][j] + 1;
                    else
                        dr[i][j] = dr[k+1][j];
                    dp[i][j] = update;
                }
            }
        }
    }

   // for (i=0; i<n; i++, std::cout << '\n')
   //     for (j=0; j<n; j++)
    //    std::cout << dp[i][j] << ' ';

    std::cout << dp[0][n-1];

    return 0;
}


#include <bits/stdc++.h>

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    int n, m, i, j, k;
    std::cin >> n;
    std::vector < int > x(n);

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

    for (i=0; i<n-1; i++){
        if (x[i] == x[i+1]){
            x.erase (x.begin()+i+1);
            x[i] = x[i] + 1;
            i = std::max (-1, i-2);
            n --;
        }
    }
    std::cout << n;

    return 0;
}


#include <bits/stdc++.h>

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    long long n, m, i, j, k;
    std::cin >> n;
    std::vector < long long > x(n+1);
    long long dp[n+1][n+1];
    long long st[n+1][n+1];
    long long dr[n+1][n+1];
    memset (dp, 0, sizeof dp);
    memset (st, 0, sizeof st);
    memset (dr, 0, sizeof dr);

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

    for (i=0; i<n; i++)
        for (j=i; j<n; j++){
            dp[i][j] = j - i + 1;
            st[i][j] = x[i];
            dr[i][j] = x[j];
        }

    long long len = 1;
    for (i=0; i<n-len; i++){
        j = i + len;
        if (x[i] == x[j]){
            dp[i][j] = 1;
            st[i][j] = x[i] + 1;
            dr[i][j] = x[j] + 1;
        }
        else
            dp[i][j] = 2;
    }

    for (len=2; len<n; len++){
        for (i=0; i<n-len; i++){
            j = i + len;
            for (k=i; k<j; k++){
                long long ok = 0, update;
                if (dr[i][k] == st[k+1][j])
                    ok = 1;
                update = dp[i][k] + dp[k+1][j] - ok;
                if (dp[i][j] > update){
                    if (dp[i][k] == 1 and ok == 1)
                        st[i][j] = st[i][k] + 1;
                    else
                        st[i][j] = st[i][k];

                    if (dp[k+1][j] == 1 and ok == 1)
                        dr[i][j] = dr[k+1][j] + 1;
                    else
                        dr[i][j] = dr[k+1][j];
                    dp[i][j] = update;
                }
            }
        }
    }

    std::cout << dp[0][n-1];

    return 0;
}


#include <bits/stdc++.h>

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    long long n, m, i, j, k;
    std::cin >> n;
    std::vector < long long > x(n+1);
    long long dp[n+1][n+1];
    long long st[n+1][n+1];
    long long dr[n+1][n+1];
    memset (dp, 0, sizeof dp);

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

    for (i=0; i<n; i++)
        for (j=i; j<n; j++){
            dp[i][j] = j - i + 1;
            st[i][j] = x[i];
            dr[i][j] = x[j];
        }

    long long len = 1;
    for (i=0; i<n-len; i++){
        j = i + len;
        if (x[i] == x[j]){
            dp[i][j] = 1;
            st[i][j] = x[i] + 1;
            dr[i][j] = x[j] + 1;
        }
        else
            dp[i][j] = 2;
    }

    for (len=2; len<n; len++){
        for (i=0; i<n-len; i++){
            j = i + len;
            for (k=i; k<j; k++){
                long long ok = 0, update;
                if (dr[i][k] == st[k+1][j])
                    ok = 1;
                update = dp[i][k] + dp[k+1][j] - ok;
                if (dp[i][j] > update){
                    if (dp[i][k] == 1)
                        st[i][j] = st[i][k] + 1;
                    else
                        st[i][j] = st[i][k];

                    if (dp[k+1][j] == 1)
                        dr[i][j] = dr[k+1][j] + 1;
                    else
                        dr[i][j] = dr[k+1][j];
                    dp[i][j] = update;
                }
            }
        }
    }

   // for (i=0; i<n; i++, std::cout << '\n')
   //     for (j=0; j<n; j++)
    //    std::cout << dp[i][j] << ' ';

    std::cout << dp[0][n-1];

    return 0;
}


#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <bitset>
#include <stdio.h>
#include <numeric>
#include <string.h>
#include <iostream>
#include <algorithm>
#define Fi first
#define Se second
#define ll long long
using namespace std;

int in(int &x,int &y,int &z){return scanf("%d%d%d",&x,&y,&z);}int in(ll &x,ll &y,ll &z){return scanf("%lld%lld%lld",&x,&y,&z);}int in(double &x,double &y,double &z){return scanf("%lf%lf%lf",&x,&y,&z);}int in(int &x,ll &y){return scanf("%d%lld",&x,&y);}int in(ll &x,int &y){return scanf("%lld%d",&x,&y);}int in(int &x,int &y){return scanf("%d%d",&x,&y);}int in(ll &x,ll &y){return scanf("%lld%lld",&x,&y);}int in(char a[],char b[]){return scanf("%s%s",a,b);}int in(double &x,double &y){return scanf("%lf%lf",&x,&y);}int in(char &c){return scanf("%c",&c);}int in(char s[]){return scanf("%s",s);}int in(int &x){return scanf("%d",&x);}int in(ll &x){return scanf("%lld",&x);}int in(double &x){return scanf("%lf",&x);}int in(pair<int,int> &p){return scanf("%d%d",&p.Fi,&p.Se);}int in(pair<ll,ll>&p){return scanf("%lld%lld",&p.Fi,&p.Se);}int in(pair<double,double> &p){return scanf("%lf%lf",&p.Fi,&p.Se);}int in(long double &a){return scanf("%Lf",&a);}int in(int &x,int &y,int &z,int &k){return scanf("%d%d%d%d",&x,&y,&z,&k);}

#define inf 0x3f3f3f3f
#define PN printf("\n")
#define lowbit(x) (x&-x)
#define ull unsigned long long
#define mmin(x,y,z) min(x,min(y,z))
#define mmax(x,y,z) max(x,max(y,z))
#define PP(x) cout<<#x<<"="<<x<<" "
#define debug(x) PP(x),PN;
#define debug2(x,y) PP(x),PP(y),PN;
#define debug3(x,y,z) PP(x),PP(y),PP(z),PN;
#define debug4(x,y,z,k) PP(x),PP(y),PP(z),PP(k),PN;
#define RunTime printf("The run time is:%.3fs\n",(double)clock()/CLOCKS_PER_SEC);
const int N=3e5+10;
const int M=1e3+10;
int dp[M*2][M],vis[M];
int check(int l,int r)
{
    for(int i=l;i<r;i++)
    {
        if(vis[l]) return 0;
    }
    return 1;
}
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x;scanf("%d",&x);
        dp[x][i]=i+1;
    }
    // debug(dp[1][1])
    int ans=1;
    for(int i=2;i<=2000;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(!dp[i][j]) dp[i][j]=dp[i-1][dp[i-1][j]];
            if(dp[i][j]) {ans=i;}
        }
    }
    // debug(ans)
    int len=n;
    for(int i=ans;i>=1;i--)
    {
        for(int j=1;j<=n;j++)
        {
            if(dp[i][j]&&check(j,dp[i][j]))
            {
                for(int k=j;k<dp[i][j];k++) vis[k]=1;
                // debug2(i,dp[i][j])
                len-=dp[i][j]-j-1,j=dp[i][j]-1;
            }
        }
    }
    // debug(ans)
    printf("%d\n",len);
    // debug(len)
    // printf("%d\n",ans);
    return 0;
}
/*
7
4 3 3 4 3 3 5
*/


#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <bitset>
#include <stdio.h>
#include <numeric>
#include <string.h>
#include <iostream>
#include <algorithm>
#define Fi first
#define Se second
#define ll long long
using namespace std;

int in(int &x,int &y,int &z){return scanf("%d%d%d",&x,&y,&z);}int in(ll &x,ll &y,ll &z){return scanf("%lld%lld%lld",&x,&y,&z);}int in(double &x,double &y,double &z){return scanf("%lf%lf%lf",&x,&y,&z);}int in(int &x,ll &y){return scanf("%d%lld",&x,&y);}int in(ll &x,int &y){return scanf("%lld%d",&x,&y);}int in(int &x,int &y){return scanf("%d%d",&x,&y);}int in(ll &x,ll &y){return scanf("%lld%lld",&x,&y);}int in(char a[],char b[]){return scanf("%s%s",a,b);}int in(double &x,double &y){return scanf("%lf%lf",&x,&y);}int in(char &c){return scanf("%c",&c);}int in(char s[]){return scanf("%s",s);}int in(int &x){return scanf("%d",&x);}int in(ll &x){return scanf("%lld",&x);}int in(double &x){return scanf("%lf",&x);}int in(pair<int,int> &p){return scanf("%d%d",&p.Fi,&p.Se);}int in(pair<ll,ll>&p){return scanf("%lld%lld",&p.Fi,&p.Se);}int in(pair<double,double> &p){return scanf("%lf%lf",&p.Fi,&p.Se);}int in(long double &a){return scanf("%Lf",&a);}int in(int &x,int &y,int &z,int &k){return scanf("%d%d%d%d",&x,&y,&z,&k);}

#define inf 0x3f3f3f3f
#define PN printf("\n")
#define lowbit(x) (x&-x)
#define ull unsigned long long
#define mmin(x,y,z) min(x,min(y,z))
#define mmax(x,y,z) max(x,max(y,z))
#define PP(x) cout<<#x<<"="<<x<<" "
#define debug(x) PP(x),PN;
#define debug2(x,y) PP(x),PP(y),PN;
#define debug3(x,y,z) PP(x),PP(y),PP(z),PN;
#define debug4(x,y,z,k) PP(x),PP(y),PP(z),PP(k),PN;
#define RunTime printf("The run time is:%.3fs\n",(double)clock()/CLOCKS_PER_SEC);
const int N=3e5+10;
const int M=1e3+10;
int dp[M*2][M],vis[M];
int check(int l,int r)
{
    for(int i=l;i<r;i++)
    {
        if(vis[l]) return 0;
    }
    return 1;
}
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x;scanf("%d",&x);
        dp[x][i]=i+1;
    }
    // debug(dp[1][1])
    int ans=1;
    for(int i=2;i<=2000;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(!dp[i][j]) dp[i][j]=dp[i-1][dp[i-1][j]];
            if(dp[i][j]) {ans=i;}
        }
    }
    // debug(ans)
    int len=n;
    for(int i=ans;i>=1;i--)
    {
        for(int j=1;j<=n;j++)
        {
            if(dp[i][j]&&check(j,dp[i][j]))
            {
                for(int k=j;k<dp[i][j];k++) vis[k]=1;
                // debug2(i,dp[i][j])
                len-=dp[i][j]-j-1,j=dp[i][j]-1;
            }
        }
    }
    // debug(ans)
    printf("%d\n",len);
    // printf("%d\n",ans);
    return 0;
}
/*
7
4 3 3 4 3 3 5
*/


#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <bitset>
#include <stdio.h>
#include <numeric>
#include <string.h>
#include <iostream>
#include <algorithm>
#define Fi first
#define Se second
#define ll long long
using namespace std;

int in(char &c) {return scanf("%c",&c);}int in(char s[]) {return scanf("%s",s);}int in(int &x) {return scanf("%d",&x);}int in(ll &x) {return scanf("%lld",&x);}int in(double &x) {return scanf("%lf",&x);}int in(pair<int,int> &p) {return scanf("%d%d",&p.Fi,&p.Se);}int in(pair<ll,ll>&p) {return scanf("%lld%lld",&p.Fi,&p.Se);}int in(pair<double,double> &p) {return scanf("%lf%lf",&p.Fi,&p.Se);}int in(long double &a) {return scanf("%Lf",&a);}/***********************/int in(int &x,ll &y) {return scanf("%d%lld",&x,&y);}int in(ll &x,int &y) {return scanf("%lld%d",&x,&y);}int in(int &x,int &y) {return scanf("%d%d",&x,&y);}int in(ll &x,ll &y) {return scanf("%lld%lld",&x,&y);}int in(int &n,char s[]){return scanf("%d%d",&n,s);}int in(char a[],char b[]) {return scanf("%s%s",a,b);}int in(double &x,double &y) {return scanf("%lf%lf",&x,&y);}/***********************/int in(int &x,int &y,int &z) {return scanf("%d%d%d",&x,&y,&z);}int in(ll &x,ll &y,ll &z) {return scanf("%lld%lld%lld",&x,&y,&z);}int in(double &x,double &y,double &z) {return scanf("%lf%lf%lf",&x,&y,&z);}/***********************/int in(int &x,int &y,int &z,int &k) {return scanf("%d%d%d%d",&x,&y,&z,&k);}int in(ll &x,ll &y,ll &z,ll &k) {return scanf("%lld%lld%lld%lld",&x,&y,&z,&k);}int in(double &x,double &y,double &z,double &k) {return scanf("%lf%lf%lf%lf",&x,&y,&z,&k);}

#define inf 0x3f3f3f3f
#define PN printf("\n");
#define lowbit(x) (x&-x)
#define ull unsigned long long
#define mmin(x,y,z) min(x,min(y,z))
#define mmax(x,y,z) max(x,max(y,z))
#define PP(x) cout<<#x<<"="<<x<<" "
#define PP2(x,y) PP(x),PP(y)
#define PP4(x,y,z,k) PP2(x,y),PP2(z,k)
#define debug(x) PP(x),PN
#define debug2(x,y) PP2(x,y),PN
#define debug3(x,y,z) PP2(x,y),PP(z),PN
#define debug4(x,y,z,k) PP4(x,y,z,k),PN
#define debug5(x,y,z,k,o) PP4(x,y,z,k),PP(o),PN
#define debug6(x,y,z,k,o,l) PP4(x,y,z,k),PP2(o,l),PN
#define RunTime printf("The run time is:%.3fs\n",(double)clock()/CLOCKS_PER_SEC);
const int N=3e5+10,M=1e3+10,mod=1e9+7;
const double eps=1e-6,Pi=acos(-1.0);
int dp[M][M],val[M][M],a[M],cnt[M][M];
int main()
{
    int n;in(n);
    memset(dp,0x3f,sizeof dp);
    memset(cnt,0x3f,sizeof cnt);

    for(int i=1;i<=n;i++)in(a[i]),dp[i][i]=0,val[i][i]=a[i],cnt[i][i]=1;
    for(int len=1;len<=n;len++){
        for(int i=1;i+len<=n;i++){
            int j=i+len;
            for(int k=i;k<j;k++){
                if(val[i][k]==val[k+1][j]&&val[i][k]!=0){
                    val[i][j]=val[i][k]+1;
                    cnt[i][j]=1;
                }
                else {
                    cnt[i][j]=min(cnt[i][j],cnt[i][k]+cnt[k+1][j]);
                }
            }
        }
    }
    printf("%d\n",cnt[1][n]);
    // debug(val[1][5])
    return 0;
}
/*

*/


#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <bitset>
#include <stdio.h>
#include <numeric>
#include <string.h>
#include <iostream>
#include <algorithm>
#define Fi first
#define Se second
#define ll long long
using namespace std;

int in(int &x,int &y,int &z){return scanf("%d%d%d",&x,&y,&z);}int in(ll &x,ll &y,ll &z){return scanf("%lld%lld%lld",&x,&y,&z);}int in(double &x,double &y,double &z){return scanf("%lf%lf%lf",&x,&y,&z);}int in(int &x,ll &y){return scanf("%d%lld",&x,&y);}int in(ll &x,int &y){return scanf("%lld%d",&x,&y);}int in(int &x,int &y){return scanf("%d%d",&x,&y);}int in(ll &x,ll &y){return scanf("%lld%lld",&x,&y);}int in(char a[],char b[]){return scanf("%s%s",a,b);}int in(double &x,double &y){return scanf("%lf%lf",&x,&y);}int in(char &c){return scanf("%c",&c);}int in(char s[]){return scanf("%s",s);}int in(int &x){return scanf("%d",&x);}int in(ll &x){return scanf("%lld",&x);}int in(double &x){return scanf("%lf",&x);}int in(pair<int,int> &p){return scanf("%d%d",&p.Fi,&p.Se);}int in(pair<ll,ll>&p){return scanf("%lld%lld",&p.Fi,&p.Se);}int in(pair<double,double> &p){return scanf("%lf%lf",&p.Fi,&p.Se);}int in(long double &a){return scanf("%Lf",&a);}int in(int &x,int &y,int &z,int &k){return scanf("%d%d%d%d",&x,&y,&z,&k);}

#define inf 0x3f3f3f3f
#define PN printf("\n")
#define lowbit(x) (x&-x)
#define ull unsigned long long
#define mmin(x,y,z) min(x,min(y,z))
#define mmax(x,y,z) max(x,max(y,z))
#define PP(x) cout<<#x<<"="<<x<<" "
#define debug(x) PP(x),PN;
#define debug2(x,y) PP(x),PP(y),PN;
#define debug3(x,y,z) PP(x),PP(y),PP(z),PN;
#define debug4(x,y,z,k) PP(x),PP(y),PP(z),PP(k),PN;
#define RunTime printf("The run time is:%.3fs\n",(double)clock()/CLOCKS_PER_SEC);
const int N=3e5+10;
const int M=1e3+10;
int dp[M*2][M],vis[M];
int check(int l,int r)
{
    for(int i=l;i<r;i++)
    {
        if(vis[i]) return 0;
    }
    return 1;
}
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x;scanf("%d",&x);
        dp[x][i]=i+1;
    }
    // debug(dp[1][1])
    int ans=1;
    for(int i=2;i<=2000;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(!dp[i][j]) dp[i][j]=dp[i-1][dp[i-1][j]];
            if(dp[i][j]) {ans=i;}
        }
    }
    // debug(ans)
    int len=n;
    for(int i=ans;i>=1;i--)
    {
        for(int j=1;j<=n;j++)
        {
            if(dp[i][j]&&check(j,dp[i][j]))
            {
                for(int k=j;k<dp[i][j];k++) vis[k]=1;
                // debug3(i,j,dp[i][j])
                len-=dp[i][j]-j-1,j=dp[i][j]-1;
            }
        }
    }
    // debug(ans)
    printf("%d\n",len);
    // debug(len)
    // printf("%d\n",ans);
    return 0;
}
/*
7
4 3 3 4 3 3 5
*/


#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <bitset>
#include <stdio.h>
#include <numeric>
#include <string.h>
#include <iostream>
#include <algorithm>
#define Fi first
#define Se second
#define ll long long
using namespace std;

int in(int &x,int &y,int &z){return scanf("%d%d%d",&x,&y,&z);}int in(ll &x,ll &y,ll &z){return scanf("%lld%lld%lld",&x,&y,&z);}int in(double &x,double &y,double &z){return scanf("%lf%lf%lf",&x,&y,&z);}int in(int &x,ll &y){return scanf("%d%lld",&x,&y);}int in(ll &x,int &y){return scanf("%lld%d",&x,&y);}int in(int &x,int &y){return scanf("%d%d",&x,&y);}int in(ll &x,ll &y){return scanf("%lld%lld",&x,&y);}int in(char a[],char b[]){return scanf("%s%s",a,b);}int in(double &x,double &y){return scanf("%lf%lf",&x,&y);}int in(char &c){return scanf("%c",&c);}int in(char s[]){return scanf("%s",s);}int in(int &x){return scanf("%d",&x);}int in(ll &x){return scanf("%lld",&x);}int in(double &x){return scanf("%lf",&x);}int in(pair<int,int> &p){return scanf("%d%d",&p.Fi,&p.Se);}int in(pair<ll,ll>&p){return scanf("%lld%lld",&p.Fi,&p.Se);}int in(pair<double,double> &p){return scanf("%lf%lf",&p.Fi,&p.Se);}int in(long double &a){return scanf("%Lf",&a);}int in(int &x,int &y,int &z,int &k){return scanf("%d%d%d%d",&x,&y,&z,&k);}

#define inf 0x3f3f3f3f
#define PN printf("\n")
#define lowbit(x) (x&-x)
#define ull unsigned long long
#define mmin(x,y,z) min(x,min(y,z))
#define mmax(x,y,z) max(x,max(y,z))
#define PP(x) cout<<#x<<"="<<x<<" "
#define debug(x) PP(x),PN;
#define debug2(x,y) PP(x),PP(y),PN;
#define debug3(x,y,z) PP(x),PP(y),PP(z),PN;
#define debug4(x,y,z,k) PP(x),PP(y),PP(z),PP(k),PN;
#define RunTime printf("The run time is:%.3fs\n",(double)clock()/CLOCKS_PER_SEC);
const int N=3e5+10;
const int M=1e3+10;
int dp[M][M*2],vis[M];
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x;scanf("%d",&x);
        dp[x][i]=i+1;
    }
    // debug(dp[1][1])
    int ans=1;
    for(int i=2;i<=2000;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(!dp[i][j]) dp[i][j]=dp[i-1][dp[i-1][j]];
            if(dp[i][j])
            {
                ans=i;
                if(ans==5) debug2(j,dp[i][j])
            }
        }
    }
    // debug(ans)
    int len=n;
    for(int i=ans;i>=1;i--)
    {
        for(int j=1;j<=n;j++)
        {
            if(dp[ans][j]&&!vis[j])
            {
                for(int k=j;k<dp[i][j];k++) vis[j]=1;
                len-=dp[i][j]-j-1,j=dp[i][j]-1;
                // debug(j)
            }
        }
    }
    printf("%d\n",len);
    // debug(len)
    // printf("%d\n",ans);
    return 0;
}
/*

*/


#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <bitset>
#include <stdio.h>
#include <numeric>
#include <string.h>
#include <iostream>
#include <algorithm>
#define Fi first
#define Se second
#define ll long long
using namespace std;

int in(int &x,int &y,int &z){return scanf("%d%d%d",&x,&y,&z);}int in(ll &x,ll &y,ll &z){return scanf("%lld%lld%lld",&x,&y,&z);}int in(double &x,double &y,double &z){return scanf("%lf%lf%lf",&x,&y,&z);}int in(int &x,ll &y){return scanf("%d%lld",&x,&y);}int in(ll &x,int &y){return scanf("%lld%d",&x,&y);}int in(int &x,int &y){return scanf("%d%d",&x,&y);}int in(ll &x,ll &y){return scanf("%lld%lld",&x,&y);}int in(char a[],char b[]){return scanf("%s%s",a,b);}int in(double &x,double &y){return scanf("%lf%lf",&x,&y);}int in(char &c){return scanf("%c",&c);}int in(char s[]){return scanf("%s",s);}int in(int &x){return scanf("%d",&x);}int in(ll &x){return scanf("%lld",&x);}int in(double &x){return scanf("%lf",&x);}int in(pair<int,int> &p){return scanf("%d%d",&p.Fi,&p.Se);}int in(pair<ll,ll>&p){return scanf("%lld%lld",&p.Fi,&p.Se);}int in(pair<double,double> &p){return scanf("%lf%lf",&p.Fi,&p.Se);}int in(long double &a){return scanf("%Lf",&a);}int in(int &x,int &y,int &z,int &k){return scanf("%d%d%d%d",&x,&y,&z,&k);}

#define inf 0x3f3f3f3f
#define PN printf("\n")
#define lowbit(x) (x&-x)
#define ull unsigned long long
#define mmin(x,y,z) min(x,min(y,z))
#define mmax(x,y,z) max(x,max(y,z))
#define PP(x) cout<<#x<<"="<<x<<" "
#define debug(x) PP(x),PN;
#define debug2(x,y) PP(x),PP(y),PN;
#define debug3(x,y,z) PP(x),PP(y),PP(z),PN;
#define debug4(x,y,z,k) PP(x),PP(y),PP(z),PP(k),PN;
#define RunTime printf("The run time is:%.3fs\n",(double)clock()/CLOCKS_PER_SEC);
const int N=3e5+10;
const int M=1e3+10;
int dp[M*2][M],vis[M];
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x;scanf("%d",&x);
        dp[x][i]=i+1;
    }
    // debug(dp[1][1])
    int ans=1;
    for(int i=2;i<=2000;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(!dp[i][j]) dp[i][j]=dp[i-1][dp[i-1][j]];
            if(dp[i][j])
            {
                ans=i;
                // if(ans==5) debug2(j,dp[i][j])
            }
        }
    }
    // debug(ans)
    int len=n;
    for(int i=ans;i>=1;i--)
    {
        for(int j=1;j<=n;j++)
        {
            if(dp[ans][j]&&!vis[j])
            {
                for(int k=j;k<dp[i][j];k++) vis[j]=1;
                len-=dp[i][j]-j-1,j=dp[i][j]-1;
                // debug(j)
            }
        }
    }
    printf("%d\n",len);
    // debug(len)
    // printf("%d\n",ans);
    return 0;
}
/*

*/


#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <bitset>
#include <stdio.h>
#include <numeric>
#include <string.h>
#include <iostream>
#include <algorithm>
#define Fi first
#define Se second
#define ll long long
using namespace std;

int in(char &c) {return scanf("%c",&c);}int in(char s[]) {return scanf("%s",s);}int in(int &x) {return scanf("%d",&x);}int in(ll &x) {return scanf("%lld",&x);}int in(double &x) {return scanf("%lf",&x);}int in(pair<int,int> &p) {return scanf("%d%d",&p.Fi,&p.Se);}int in(pair<ll,ll>&p) {return scanf("%lld%lld",&p.Fi,&p.Se);}int in(pair<double,double> &p) {return scanf("%lf%lf",&p.Fi,&p.Se);}int in(long double &a) {return scanf("%Lf",&a);}/***********************/int in(int &x,ll &y) {return scanf("%d%lld",&x,&y);}int in(ll &x,int &y) {return scanf("%lld%d",&x,&y);}int in(int &x,int &y) {return scanf("%d%d",&x,&y);}int in(ll &x,ll &y) {return scanf("%lld%lld",&x,&y);}int in(int &n,char s[]){return scanf("%d%d",&n,s);}int in(char a[],char b[]) {return scanf("%s%s",a,b);}int in(double &x,double &y) {return scanf("%lf%lf",&x,&y);}/***********************/int in(int &x,int &y,int &z) {return scanf("%d%d%d",&x,&y,&z);}int in(ll &x,ll &y,ll &z) {return scanf("%lld%lld%lld",&x,&y,&z);}int in(double &x,double &y,double &z) {return scanf("%lf%lf%lf",&x,&y,&z);}/***********************/int in(int &x,int &y,int &z,int &k) {return scanf("%d%d%d%d",&x,&y,&z,&k);}int in(ll &x,ll &y,ll &z,ll &k) {return scanf("%lld%lld%lld%lld",&x,&y,&z,&k);}int in(double &x,double &y,double &z,double &k) {return scanf("%lf%lf%lf%lf",&x,&y,&z,&k);}

#define inf 0x3f3f3f3f
#define PN printf("\n");
#define lowbit(x) (x&-x)
#define ull unsigned long long
#define mmin(x,y,z) min(x,min(y,z))
#define mmax(x,y,z) max(x,max(y,z))
#define PP(x) cout<<#x<<"="<<x<<" "
#define PP2(x,y) PP(x),PP(y)
#define PP4(x,y,z,k) PP2(x,y),PP2(z,k)
#define debug(x) PP(x),PN
#define debug2(x,y) PP2(x,y),PN
#define debug3(x,y,z) PP2(x,y),PP(z),PN
#define debug4(x,y,z,k) PP4(x,y,z,k),PN
#define debug5(x,y,z,k,o) PP4(x,y,z,k),PP(o),PN
#define debug6(x,y,z,k,o,l) PP4(x,y,z,k),PP2(o,l),PN
#define RunTime printf("The run time is:%.3fs\n",(double)clock()/CLOCKS_PER_SEC);
const int N=3e5+10,M=1e3+10,mod=1e9+7;
const double eps=1e-6,Pi=acos(-1.0);
int dp[M][M],val[M][M],a[M],cnt[M][M];
int main()
{
    int n;in(n);
    memset(dp,0x3f,sizeof dp);
    memset(cnt,0x3f,sizeof cnt);

    for(int i=1;i<=n;i++)in(a[i]),dp[i][i]=0,val[i][i]=a[i],cnt[i][i]=1;
    for(int len=1;len<=n;len++){
        for(int i=1;i+len<=n;i++){
            int j=i+len;
            for(int k=i;k<j;k++){
                if(dp[i][k]+dp[k+1][j]<=100){
                    if(val[i][k]==val[k+1][j]){
                        dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+1);
                        val[i][j]=val[i][k]+1;
                        cnt[i][j]=1;
                    }
                    else {
                        cnt[i][j]=min(cnt[i][j],cnt[i][k]+cnt[k+1][j]);
                    }
                }
                else
                cnt[i][j]=min(cnt[i][j],cnt[i][k]+cnt[k+1][j]);
            }
        }
    }
    printf("%d\n",cnt[1][n]);
    // debug(val[1][5])
    return 0;
}
/*

*/


#include <bits/stdc++.h>

using namespace std;

const double PI = 3.141592653589;
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define int long long
#define all(c) (c).begin(),(c).end()
#define M 1000000007
#define INF LLONG_MAX
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ", "; dbs(str.substr(idx + 1), s...);}
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void prc(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;} cerr << "]\n";}
// Use pr(a,b,c,d,e) or cerr<<anything or prc(v.begin(),v.end()) or prc(v,v+n)
//

int hsh(vector<int> vec) {
  int seed = vec.size();
  for(auto& i : vec) {
    seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  }
  return seed;
}

map<int, bool> used;
int f(vector<int>& v, int n)
{
    // min value that is same
	int z = hsh(v);
    if(used[z]) return M;
    used[z] = true;
    int ans = n,last_used = -1;
    for(int i=0;i<n-1;i++)
    {
        if(v[i] == v[i+1])
        {
            if(i != 0 and i != n-1)
            {
                // only left and right matters
                if(v[i-1] == v[i] and v[i+2] == v[i]) continue;
            }
            vector<int> temp(n-1);
            for(int j=0;j<i;j++)
            {
                temp[j] = v[j];
            }
            temp[i] = v[i] + 1;
            for(int j=i+2;j<n;j++)
                temp[j-1] = v[j];
            ans = min(ans, f(temp, n-1));

        }

    }
    return ans;
}

int32_t main()
{
    fastio;
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);

    int n;
    cin >> n;
    vector<int> v(n);
    for(int i=0;i<n;i++) cin >> v[i];

    cout << f(v, n) << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const double PI = 3.141592653589;
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define int long long
#define all(c) (c).begin(),(c).end()
#define M 1000000007
#define INF LLONG_MAX
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ", "; dbs(str.substr(idx + 1), s...);}
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void prc(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;} cerr << "]\n";}
// Use pr(a,b,c,d,e) or cerr<<anything or prc(v.begin(),v.end()) or prc(v,v+n)
//

map<vector<int>, bool> used;
int f(vector<int>& v, int n)
{
    // min value that is same
    if(used[v]) return M;
    used[v] = true;
    int ans = n;
    for(int i=0;i<n-1;i++)
    {
        if(v[i] == v[i+1])
        {
            vector<int> temp(n-1);
            for(int j=0;j<i;j++)
            {
                temp[j] = v[j];
            }
            temp[i] = v[i] + 1;
            for(int j=i+2;j<n;j++)
                temp[j-1] = v[j];
            ans = min(ans, f(temp, n-1));

        }

    }
    return ans;
}

int32_t main()
{
    fastio;
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);

    int n;
    cin >> n;
    vector<int> v(n);
    for(int i=0;i<n;i++) cin >> v[i];

    cout << f(v, n) << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const double PI = 3.141592653589;
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define int long long
#define all(c) (c).begin(),(c).end()
#define M 1000000007
#define INF LLONG_MAX
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ", "; dbs(str.substr(idx + 1), s...);}
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void prc(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;} cerr << "]\n";}
// Use pr(a,b,c,d,e) or cerr<<anything or prc(v.begin(),v.end()) or prc(v,v+n)
//

int hsh(vector<int> vec) {
  int seed = vec.size();
  for(auto& i : vec) {
    seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  }
  return seed;
}

map<int, bool> used;
int f(vector<int>& v, int n)
{
    // min value that is same
	int z = hsh(v);
    if(used[z]) return M;
    used[z] = true;
    int ans = n;
    for(int i=0;i<n-1;i++)
    {
        if(v[i] == v[i+1])
        {
            vector<int> temp(n-1);
            for(int j=0;j<i;j++)
            {
                temp[j] = v[j];
            }
            temp[i] = v[i] + 1;
            for(int j=i+2;j<n;j++)
                temp[j-1] = v[j];
            ans = min(ans, f(temp, n-1));

        }

    }
    return ans;
}

int32_t main()
{
    fastio;
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);

    int n;
    cin >> n;
    vector<int> v(n);
    for(int i=0;i<n;i++) cin >> v[i];

    cout << f(v, n) << "\n";
    return 0;
}


// for explanations: https://www.youtube.com/watch?v=ytOManO7GO0
#include <bits/stdc++.h>

using namespace std;

const double PI = 3.141592653589;
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define int long long
#define all(c) (c).begin(),(c).end()
#define M 1000000007
#define INF LLONG_MAX
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ", "; dbs(str.substr(idx + 1), s...);}
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void prc(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;} cerr << "]\n";}
// Use pr(a,b,c,d,e) or cerr<<anything or prc(v.begin(),v.end()) or prc(v,v+n)
//

int32_t main()
{
    fastio;
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i=0;i<n;i++)
        cin >> a[i];
    vector<vector<int>> dp(n, vector<int>(n, -1));
    for(int i=0;i<n;i++) dp[i][i] = a[i];
    for(int i=n-1;i>=0;i--)
    {
        for(int j=i+1;j<n;j++)
        {
            for(int k=i;k<j;k++)
            {
                if(dp[i][k] == dp[k+1][j] and dp[i][k] != -1)
                {
                    dp[i][j] = dp[i][k] + 1;
                }
            }
        }
    }
    vector<int> pre(n, M);
    for(int i=0;i<n;i++)
    {
        for(int j=i;j<n;j++)
        {
            if(dp[i][j] != -1)
            {
                if(i)
                    pre[j] = min(pre[i-1] + 1, pre[j]);
                else pre[j] = 1;
            }
        }
    }
    cout << pre[n-1] << "\n";

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define f(i, j, k) for(ll i = j; i < ll(k); i++)
#define fr(i, j, k) for(ll i = j; i >= (k); i--)
#define wch(x) cout << (#x) << " " << x << endl
#define fio ios::sync_with_stdio(0);cin.tie(NULL)
#define pb(a) push_back(a)
#define m(a) memset(a, 0, sizeof(a))
#define m1(a) memset(a, -1, sizeof(a))
#define mp(a, b) make_pair(a, b)
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vi vector<ll>
#define sec second
#define pres(n) fixed << setprecision(n)
#define fst first
#define all(v) v.begin(), v.end()
#define mxe(a) (*max_element(all(a)))
//#define c2(n) (1LL * ((n)*1LL * ((n)-1)) / 2)
#define vv(type) vector<vector<type>>
const int mn = INT_MIN;
const int mx = INT_MAX;
const ll bmod= 2305843009213693951;
const ll mod = 1e9 + 7;
double poi = 3.141592653589793238463;
//
double absd(double v)
{return (v >= 0) ? v : -v;}
//
void prt(ll n)
{while(n) {cout << (n & 1);n = n >> 1;}cout << '\n';}
//
ll pw(ll a, ll p, ll m = mod)
{
    ll res = 1;
    while(p > 0) {
        if(p & 1)
            res = (res * a) % m;
        a = (a * a) % m;
        p >>= 1;
    }
    return res;
}
//
ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);}
/////////////////////////////
/////////////////////////////
/////////////////////////////
const int sz = (1e3) + 10;
ll jada = 1e9;

bool dp[sz][sz];
int dp1[sz];
int stree[4*sz];
int a[sz];

void build(int curr,int s,int e) {
    if(s==e) {stree[curr]=a[s];return;}
    int m=(s+e)/2;
    build(2*curr,s,m);
    build(2*curr+1,m+1,e);
    stree[curr]=min(stree[2*curr],stree[2*curr+1]);
    }

int rmq(int curr,int s,int e,int rs,int re) {
    if(rs>e || re<s )  return mx;
    if(s>=rs && e<=re) return stree[curr];
    int m=(s+e)/2;
    int lq=rmq(2*curr,s,m,rs,re);
    int rq=rmq(2*curr+1,m+1,e,rs,re);
    return min(lq,rq);
    }

int main()
{
    //fio;
    m(dp);
    m(dp1);
    int n;
    cin>>n;
    f(i,1,n+1) cin>>a[i];
    build(1,1,n);
    //wch(rmq(1,1,n,1,4));
    f(len,1,n+1) {
        f(i,1,n+1) {
            dp[i][i]=1;
            int j=i+len-1;
            f(k,i+1,j+1) {
                int lq=rmq(1,1,n,i,k-1);
                int rq=rmq(1,1,n,k,j);
                if(((lq+(k-i-1)) == (rq+(j-k))) && dp[i][k-1] && dp[k][j]) dp[i][j]=1;
                }
            }
        }
    //f(i,1,n+1) f(j,1,n+1) wch(dp[i][j]);
    f(i,1,n+1) {
        dp1[i]=i;
        f(j,1,i+1) {
            if(dp[j][i]) dp1[i]=min(dp1[i],dp1[j-1]+1);
            }
        }
    cout<<dp1[n];
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define f(i, j, k) for(ll i = j; i < ll(k); i++)
#define fr(i, j, k) for(ll i = j; i >= (k); i--)
#define wch(x) cout << (#x) << " " << x << endl
#define fio ios::sync_with_stdio(0);cin.tie(NULL)
#define pb(a) push_back(a)
#define m(a) memset(a, 0, sizeof(a))
#define m1(a) memset(a, -1, sizeof(a))
#define mp(a, b) make_pair(a, b)
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vi vector<ll>
#define sec second
#define pres(n) fixed << setprecision(n)
#define fst first
#define all(v) v.begin(), v.end()
#define mxe(a) (*max_element(all(a)))
//#define c2(n) (1LL * ((n)*1LL * ((n)-1)) / 2)
#define vv(type) vector<vector<type>>
const int mn = INT_MIN;
const int mx = INT_MAX;
const ll bmod= 2305843009213693951;
const ll mod = 1e9 + 7;
double poi = 3.141592653589793238463;
//
double absd(double v)
{return (v >= 0) ? v : -v;}
//
void prt(ll n)
{while(n) {cout << (n & 1);n = n >> 1;}cout << '\n';}
//
ll pw(ll a, ll p, ll m = mod)
{
    ll res = 1;
    while(p > 0) {
        if(p & 1)
            res = (res * a) % m;
        a = (a * a) % m;
        p >>= 1;
    }
    return res;
}
//
ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);}
/////////////////////////////
/////////////////////////////
/////////////////////////////
const int sz = (1e3) + 10;
ll jada = 1e9;

bool dp[sz][sz];
int dp1[sz];
int stree[4*sz];
int a[sz];

void build(int curr,int s,int e) {
    if(s==e) {stree[curr]=a[s];return;}
    int m=(s+e)/2;
    build(2*curr,s,m);
    build(2*curr+1,m+1,e);
    stree[curr]=min(stree[2*curr],stree[2*curr+1]);
    }

int rmq(int curr,int s,int e,int rs,int re) {
    if(rs>e || re<s )  return mx;
    if(s>=rs && e<=re) return stree[curr];
    int m=(s+e)/2;
    int lq=rmq(2*curr,s,m,rs,re);
    int rq=rmq(2*curr+1,m+1,e,rs,re);
    return min(lq,rq);
    }

int main()
{
    fio;
    m(dp);
    m(dp1);
    int n;
    cin>>n;
    f(i,1,n+1) cin>>a[i];
    build(1,1,n);
    //wch(rmq(1,1,n,1,4));
    f(len,1,n+1) {
        f(i,1,n+1) {
            dp[i][i]=1;
            if(len==1) continue;
            int j=i+len-1;
            if(j>n) continue;
            f(k,i+1,j+1) {
                int lq=rmq(1,1,n,i,k-1);
                int rq=rmq(1,1,n,k,j);
                if(((lq+(k-i-1)) == (rq+(j-k))) && dp[i][k-1] && dp[k][j]) dp[i][j]=1;
                }
            }
        }
    //f(i,1,n+1) f(j,1,n+1) wch(dp[i][j]);
    f(i,1,n+1) {
        dp1[i]=i;
        f(j,1,i+1) {
            if(dp[j][i]) dp1[i]=min(dp1[i],dp1[j-1]+1);
            }
        }
    cout<<dp1[n];
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define f(i, j, k) for(ll i = j; i < ll(k); i++)
#define fr(i, j, k) for(ll i = j; i >= (k); i--)
#define wch(x) cout << (#x) << " " << x << endl
#define fio ios::sync_with_stdio(0);cin.tie(NULL)
#define pb(a) push_back(a)
#define m(a) memset(a, 0, sizeof(a))
#define m1(a) memset(a, -1, sizeof(a))
#define mp(a, b) make_pair(a, b)
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vi vector<ll>
#define sec second
#define pres(n) fixed << setprecision(n)
#define fst first
#define all(v) v.begin(), v.end()
#define mxe(a) (*max_element(all(a)))
//#define c2(n) (1LL * ((n)*1LL * ((n)-1)) / 2)
#define vv(type) vector<vector<type>>
const int mn = INT_MIN;
const int mx = INT_MAX;
const ll bmod= 2305843009213693951;
const ll mod = 1e9 + 7;
double poi = 3.141592653589793238463;
//
double absd(double v)
{return (v >= 0) ? v : -v;}
//
void prt(ll n)
{while(n) {cout << (n & 1);n = n >> 1;}cout << '\n';}
//
ll pw(ll a, ll p, ll m = mod)
{
    ll res = 1;
    while(p > 0) {
        if(p & 1)
            res = (res * a) % m;
        a = (a * a) % m;
        p >>= 1;
    }
    return res;
}
//
ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);}
/////////////////////////////
/////////////////////////////
/////////////////////////////
const int sz = (1e3) + 10;
ll jada = 1e9;

bool dp[sz][sz];
int dp1[sz];
int stree[4*sz];
int a[sz];

void build(int curr,int s,int e) {
    if(s==e) {stree[curr]=a[s];return;}
    int m=(s+e)/2;
    build(2*curr,s,m);
    build(2*curr+1,m+1,e);
    stree[curr]=min(stree[2*curr],stree[2*curr+1]);
    }

int rmq(int curr,int s,int e,int rs,int re) {
    if(rs>e || re<s )  return mx;
    if(s>=rs && e<=re) return stree[curr];
    int m=(s+e)/2;
    int lq=rmq(2*curr,s,m,rs,re);
    int rq=rmq(2*curr+1,m+1,e,rs,re);
    return min(lq,rq);
    }

int main()
{
    //fio;
    m(dp);
    m(dp1);
    int n;
    cin>>n;
    f(i,1,n+1) cin>>a[i];
    build(1,1,n);
    //wch(rmq(1,1,n,1,4));
    f(len,1,n+1) {
        f(i,1,n+1) {
            dp[i][i]=1;
            if(len==1) continue;
            int j=i+len-1;
            f(k,i+1,j+1) {
                int lq=rmq(1,1,n,i,k-1);
                int rq=rmq(1,1,n,k,j);
                if(((lq+(k-i-1)) == (rq+(j-k))) && dp[i][k-1] && dp[k][j]) dp[i][j]=1;
                }
            }
        }
    //f(i,1,n+1) f(j,1,n+1) wch(dp[i][j]);
    f(i,1,n+1) {
        dp1[i]=i;
        f(j,1,i+1) {
            if(dp[j][i]) dp1[i]=min(dp1[i],dp1[j-1]+1);
            }
        }
    cout<<dp1[n];
}


#include <iostream>
#include <tuple>
using namespace std;

#define tup tuple<int,int,int>
#define mt make_tuple
#define g0 get<0>
#define g1 get<1>
#define g2 get<2>

const int N = 2e6 + 5;

string s;
int m,l,r;
int eq[N];
int op[N];
int cl[N];

void build(int id, int l, int r){
	if(l==r){
		if(s[l]=='(')op[id]++;
		else cl[id]++;
		return;
	}

	int mid = (l+r)/2;
	build(id*2,l,mid);
	build(id*2+1,mid+1,r);

	int more = min(op[id*2], cl[id*2+1]);
	op[id] = op[2*id] + op[2*id + 1] - more;
	cl[id] = cl[2*id] + cl[2*id + 1] - more;
	eq[id] = eq[2*id] + eq[2*id + 1] + more;

}

// eq, op, cl
tup get(int id, int l, int r, int u, int v){
	if(r < u || l > v) return mt(0,0,0);

	if(l >= u && r <= v) return mt(eq[id],op[id],cl[id]);

	int mid = (l + r) / 2;
	tup tup1 = get(id*2,l,mid,u,v);
	tup tup2 = get(id*2+1,mid+1,r,u,v);

	int more = min(g1(tup1), g2(tup2));

	return mt(g0(tup1) + g0(tup2) + more, g1(tup1) + g1(tup2) - more, g2(tup1) + g2(tup2) - more);
}

void doit(){
	cin >> s;
	build(1,0,s.length()-1);

	cin >> m;
	while(m--){
		cin >> l >> r;
		tup rtup = get(1,0,s.length()-1,l-1,r-1);
		cout << 2 * g0(rtup) << endl;
	}
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	doit();
	return 0;
}


/*Stop Wasting Time!!!*/
#include<bits/stdc++.h>
using namespace std;
//#define int long long
namespace Fread {
	const int MAXN=1<<20;char buf[MAXN],*S,*T;
	inline char getchar() {if (S==T) {T=(S=buf)+fread(buf,1,MAXN,stdin);if (S==T) return EOF;}return *S++;}
}
inline int read() {
	int x(0),neg(1);char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') neg=-1;ch=getchar();}
	while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch-'0');ch=getchar();}
	return x*neg;
}
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
#endif
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
string str;
const int MAXN=1e6;
int n;
namespace Segt{
	struct Tree{
		int ll,rr;
	}tr[MAXN*4+5];
	inline void Pushup(int p) {
		tr[p].ll=tr[p<<1].ll+tr[p<<1|1].ll-min(tr[p<<1].ll,tr[p<<1|1].rr);
		tr[p].rr=tr[p<<1].rr+tr[p<<1|1].rr-min(tr[p<<1].ll,tr[p<<1|1].rr);
	}
	inline void Build(int p,int l,int r) {
		if (l==r) {
			tr[p].ll=(str[l-1]=='(');
			tr[p].rr=(str[l-1]==')');
			return ;
		}
		int mid=l+r>>1;
		Build(p<<1,l,mid);
		Build(p<<1|1,mid+1,r);
		Pushup(p);
	}
	inline Tree Query(int p,int l,int r,int x,int y) {
		if (l>=x && y>=r) {
			return (Tree){tr[p].ll,tr[p].rr};
		}
		int mid=l+r>>1,res=0ll;
		if (x<=mid && y<=mid) {
			return Query(p<<1,l,mid,x,y);
		}
		else if (x>mid && y>mid) {
			return Query(p<<1|1,mid+1,r,x,y);
		}
		else {
			Tree nd1=Query(p<<1,l,mid,x,y),nd2=Query(p<<1|1,mid+1,r,x,y),res={0,0};
			res.ll=nd1.ll+nd2.ll-min(nd1.ll,nd2.rr);
			res.rr=nd1.rr+nd2.rr-min(nd1.ll,nd2.rr);
			return res;
		}
	}
}
using namespace Segt;
signed main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	int m=0;
	cin>>str>>m;
	n=str.size();
	Build(1,1,n);
	for (int i=1;i<=m;++i) {
		int l,r;
		cin>>l>>r;
		Tree c=Query(1,1,n,l,r);
		printf("%d\n",r-l+1-c.ll-c.rr);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
namespace Fread {
	const int MAXN=1<<20;
	char buf[MAXN],*S,*T;
	inline char getchar() {
		if (S==T) {
			T=(S=buf)+fread(buf,1,MAXN,stdin);
			if (S==T) return EOF;
		}
		return *S++;
	}
}
inline int read() {
	int x(0),neg(1);
	char ch=getchar();
	while(!isdigit(ch)) {
		if (ch=='-') neg=-1;
		ch=getchar();
	}
	while(isdigit(ch)) {
		x=(x<<1)+(x<<3)+(ch-'0');
		ch=getchar();
	}
	return x*neg;
}
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
#endif
#define pb push_back
const int MAXN=1000000;
int m;
char s[MAXN+5];
namespace Segt{
	struct Tree{
		int l,r,v,lv,rv;
	}tree[MAXN<<2+5];
	inline void pushup(int p) {
		int minv=min(tree[p<<1].lv,tree[p<<1|1].rv);
		tree[p].lv=tree[p<<1].lv+tree[p<<1|1].lv-minv;
		tree[p].rv=tree[p<<1].rv+tree[p<<1|1].rv-minv;
		tree[p].v=tree[p].r-tree[p].l+1-tree[p].lv-tree[p].rv;
	}
	inline void build(int p,int l,int r) {
		tree[p].l=l,tree[p].r=r;
		if (l==r) {
			tree[p].lv=s[l]=='('?1:0;
			tree[p].rv=s[l]==')'?1:0;
			tree[p].v=0;
			return ;
		}
		int mid=l+r>>1;
		build(p<<1,l,mid);
		build(p<<1|1,mid+1,r);
		pushup(p);
	}
	inline Tree query(int p,int x,int y) {
		if (tree[p].l>=x && tree[p].r<=y) {
			return tree[p];
		}
		int mid=tree[p].l+tree[p].r>>1;
		if (x<=mid && y<=mid) {
			return query(p<<1,x,y);
		}
		if (x>mid && y>mid) {
			return query(p<<1|1,x,y);
		}
		Tree T1=query(p<<1,x,y),T2=query(p<<1|1,x,y),Tans;
		int minv=min(T1.lv,T2.rv);
		Tans.lv=T1.lv+T2.lv-minv;
		Tans.rv=T1.rv+T2.rv-minv;
		return Tans;
	}
}
using namespace Segt;
signed main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	scanf("%s",s+1);
	int n=strlen(s+1);
	scanf("%d",&m);
	build(1,1,n);
	for (int i=1;i<=m;++i) {
		int l,r;
		scanf("%d%d",&l,&r);
		Tree Ans=query(1,l,r);
		printf("%d\n",(r-l+1)-Ans.lv-Ans.rv);
	}
	return 0;
}


/*Stop Wasting Time!!!*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace Fread {
	const int MAXN=1<<20;char buf[MAXN],*S,*T;
	inline char getchar() {if (S==T) {T=(S=buf)+fread(buf,1,MAXN,stdin);if (S==T) return EOF;}return *S++;}
}
inline int read() {
	int x(0),neg(1);char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') neg=-1;ch=getchar();}
	while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch-'0');ch=getchar();}
	return x*neg;
}
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
#endif
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
string str;
const int MAXN=1e6;
int n;
namespace Segt{
	struct Tree{
		int ll,rr;
	}tr[MAXN*4+5];
	inline void Pushup(int p) {
		tr[p].ll=tr[p<<1].ll+tr[p<<1|1].ll-min(tr[p<<1].ll,tr[p<<1|1].rr);
		tr[p].rr=tr[p<<1].rr+tr[p<<1|1].rr-min(tr[p<<1].ll,tr[p<<1|1].rr);
	}
	inline void Build(int p,int l,int r) {
		if (l==r) {
			tr[p].ll=(str[l-1]=='(');
			tr[p].rr=(str[l-1]==')');
			return ;
		}
		int mid=l+r>>1;
		Build(p<<1,l,mid);
		Build(p<<1|1,mid+1,r);
		Pushup(p);
	}
	inline Tree Query(int p,int l,int r,int x,int y) {
		if (l>=x && y>=r) {
			return (Tree){tr[p].ll,tr[p].rr};
		}
		int mid=l+r>>1,res=0ll;
		if (x<=mid && y<=mid) {
			return Query(p<<1,l,mid,x,y);
		}
		else if (x>mid && y>mid) {
			return Query(p<<1|1,mid+1,r,x,y);
		}
		else {
			Tree nd1=Query(p<<1,l,mid,x,y),nd2=Query(p<<1|1,mid+1,r,x,y),res={0,0};
			res.ll=nd1.ll+nd2.ll-min(nd1.ll,nd2.rr);
			res.rr=nd1.rr+nd2.rr-min(nd1.ll,nd2.rr);
			return res;
		}
	}
}
using namespace Segt;
signed main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	int m=0;
	cin>>str>>m;
	n=str.size();
	Build(1,1,n);
	for (int i=1;i<=m;++i) {
		int l=read(),r=read();
		Tree c=Query(1,1,n,l,r);
		printf("%lld\n",r-l+1-c.ll-c.rr);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
namespace Fread {
	const int MAXN=1<<20;
	char buf[MAXN],*S,*T;
	inline char getchar() {
		if (S==T) {
			T=(S=buf)+fread(buf,1,MAXN,stdin);
			if (S==T) return EOF;
		}
		return *S++;
	}
}
inline int read() {
	int x(0),neg(1);
	char ch=getchar();
	while(!isdigit(ch)) {
		if (ch=='-') neg=-1;
		ch=getchar();
	}
	while(isdigit(ch)) {
		x=(x<<1)+(x<<3)+(ch-'0');
		ch=getchar();
	}
	return x*neg;
}
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
#endif
#define pb push_back
const int MAXN=1000000;
int m;
char s[MAXN+5];
namespace Segt{
	struct Tree{
		int l,r,v,lv,rv;
	}tree[MAXN<<1+5];
	inline void pushup(int p) {
		int minv=min(tree[p<<1].lv,tree[p<<1|1].rv);
		tree[p].lv=tree[p<<1].lv+tree[p<<1|1].lv-minv;
		tree[p].rv=tree[p<<1].rv+tree[p<<1|1].rv-minv;
		tree[p].v=tree[p].r-tree[p].l+1-tree[p].lv-tree[p].rv;
	}
	inline void build(int p,int l,int r) {
		tree[p].l=l,tree[p].r=r;
		if (l==r) {
			tree[p].lv=s[l]=='('?1:0;
			tree[p].rv=s[l]==')'?1:0;
			tree[p].v=0;
			return ;
		}
		int mid=l+r>>1;
		build(p<<1,l,mid);
		build(p<<1|1,mid+1,r);
		pushup(p);
	}
	inline Tree query(int p,int x,int y) {
		if (tree[p].l>=x && tree[p].r<=y) {
			return tree[p];
		}
		int mid=tree[p].l+tree[p].r>>1;
		if (x<=mid && y<=mid) {
			return query(p<<1,x,y);
		}
		if (x>mid && y>mid) {
			return query(p<<1|1,x,y);
		}
		Tree T1=query(p<<1,x,y),T2=query(p<<1|1,x,y),Tans;
		int minv=min(T1.lv,T2.rv);
		Tans.lv=T1.lv+T2.lv-minv;
		Tans.rv=T1.rv+T2.rv-minv;
		return Tans;
	}
}
using namespace Segt;
signed main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	scanf("%s",s+1);
	int n=strlen(s+1);
	scanf("%d",&m);
	build(1,1,n);
	for (int i=1;i<=m;++i) {
		int l,r;
		scanf("%d%d",&l,&r);
		Tree Ans=query(1,l,r);
		printf("%d\n",(r-l+1)-Ans.lv-Ans.rv);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
namespace Fread {
	const int MAXN=1<<20;
	char buf[MAXN],*S,*T;
	inline char getchar() {
		if (S==T) {
			T=(S=buf)+fread(buf,1,MAXN,stdin);
			if (S==T) return EOF;
		}
		return *S++;
	}
}
inline int read() {
	int x(0),neg(1);
	char ch=getchar();
	while(!isdigit(ch)) {
		if (ch=='-') neg=-1;
		ch=getchar();
	}
	while(isdigit(ch)) {
		x=(x<<1)+(x<<3)+(ch-'0');
		ch=getchar();
	}
	return x*neg;
}
#ifdef ONLINE_JUDGE
	#define getchar Fread::getchar
#endif
#define pb push_back
const int MAXN=1000000;
int m;
char s[MAXN+5];
namespace Segt{
	struct Tree{
		int lv,rv;
	}tree[MAXN<<2+5];
	inline void pushup(int p) {
		int minv=min(tree[p<<1].lv,tree[p<<1|1].rv);
		tree[p].lv=tree[p<<1].lv+tree[p<<1|1].lv-minv;
		tree[p].rv=tree[p<<1].rv+tree[p<<1|1].rv-minv;
	}
	inline void build(int p,int l,int r) {
		if (l==r) {
			tree[p].lv=s[l]=='('?1:0;
			tree[p].rv=s[l]==')'?1:0;
			return ;
		}
		int mid=l+r>>1;
		build(p<<1,l,mid);
		build(p<<1|1,mid+1,r);
		pushup(p);
	}
	inline Tree query(int p,int l,int r,int x,int y) {
		if (l>=x && r<=y) {
			return tree[p];
		}
		int mid=l+r>>1;
		if (x<=mid && y<=mid) {
			return query(p<<1,l,mid,x,y);
		}
		if (x>mid && y>mid) {
			return query(p<<1|1,mid+1,r,x,y);
		}
		Tree T1=query(p<<1,l,mid,x,y),T2=query(p<<1|1,mid+1,r,x,y),Tans;
		int minv=min(T1.lv,T2.rv);
		Tans.lv=T1.lv+T2.lv-minv;
		Tans.rv=T1.rv+T2.rv-minv;
		return Tans;
	}
}
using namespace Segt;
signed main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	scanf("%s",s+1);
	int n=strlen(s+1);
	scanf("%d",&m);
	build(1,1,n);
	for (int i=1;i<=m;++i) {
		int l,r;
		scanf("%d%d",&l,&r);
		Tree Ans=query(1,1,n,l,r);
		printf("%d\n",(r-l+1)-Ans.lv-Ans.rv);
	}
	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

class FastIO{
public:
    inline FastIO & operator << (const int &a){ printf("%d" , a); return *this; }
    inline FastIO & operator << (const char *a){ printf("%s" , a); return *this; }
    inline FastIO & operator >> (const int &a){ scanf("%d" , &a); return *this; }
    inline FastIO & operator >> (const char * const&a){ scanf("%s" , a); return *this; }
}hir;

struct node{
    int ans , op , cl;
    node():
        ans(0) , op(0) , cl(0) {};

};

const int N = 1e6+1 , LG = 20;

node lr[N][LG];
char s[N];
node merg(node *a , node *b){
    node ret;
    int mn = std::min(a->op , b->cl );
    ret.ans = 2*mn + a->ans + b->ans;
    ret.op = a->op + b->op - mn;
    ret.cl = a->cl + b->cl - mn;
    return ret;
}

node get_par(int v , int cnt){
    node ans;
    for(int p=0 ; p<LG ; p++)
        if( cnt & (1<<p) ){
            ans = merg(&lr[v][p] , &ans);
            v = v - (1<<p);
        }
    return ans;
}

int main(){
    node a , b;
    hir >> s;
    int n=0;
    while( s[n] != NULL ) n++;

    for(int i=0 ; i<=n ; i++)
        for(int j=0 ; j<LG ; j++)
            lr[i][j] = node();

    for(int i=0 ; i<=n ; i++){
        if(i == 0) continue;
        node cur;
        if(s[i-1] == '(') cur.op++; else cur.cl++;
        lr[i][0] = cur;
        for(int bit=1 ; bit<LG ; bit++){
            if( i - (1<<(bit-1)) < 0 ) continue;
            lr[i][bit] = merg( &lr[ i - (1<<(bit-1)) ][bit-1] , &lr[i][bit-1] );
        }
    }
    int q;hir >> q;
    for(int i=0 ; i<q ; i++){
        int l ,r;hir >> l >> r;l--;
        hir << get_par(r , r-l).ans << "\n";
    }
}


#include <bits/stdc++.h>

//#define endl "\n"

using namespace std;

typedef long long ll;
typedef pair< ll , ll> ii;

const int  N = 1e6+10;

string s;

struct Node{
	int l , r , cntopen , cntclose , ans;
	Node():
		l(-1) , r(-1) , ans(0) , cntopen(0) , cntclose(0) {}
};

struct segtree{
	Node node[4*N + 100];

	Node merg(Node left , Node right){
		Node ans;
		int mn = min( left.cntopen , right.cntclose );
		ans.ans = left.ans + right.ans + 2*mn;
		ans.cntopen = left.cntopen + right.cntopen - mn;
		ans.cntclose = left.cntclose + right.cntclose - mn;
		return ans;
	}

	void build(int l , int r , int index){
		if(r == l){
			node[index].cntopen = s[r] == '(' ? 1 : 0;
			node[index].cntclose = s[r] == ')' ? 1 : 0;
			node[index].l = l;
			node[index].r = r;
			return;
		}
		int mid = (l+r)/2;
		build(l , mid , 2*index);
		build(mid+1 , r , 2*index + 1);
		node[index] = merg( node[2*index]  , node[2*index + 1] );
		node[index].l = l;
		node[index].r = r;
	}
	Node ask(int lq , int rq , int index){
		int l = node[index].l , r = node[index].r;
		if(lq > r || l > rq)
			return Node();
		if(l >= lq && r <= rq)
			return node[index];
		int mid = (l + r) / 2;

		//Node ne = merg( ask(lq , rq , 2*index) , ask(lq , rq , 2*index+1) );
		return merg( ask(lq , rq , 2*index) , ask(lq , rq , 2*index+1) );
	}

};

int main(){
	ios_base::sync_with_stdio( false );cin.tie(0);cout.tie(0);
	cin >> s;
	int n = s.size();
	segtree *st = new segtree();
	st->build(0 , n-1 , 1);
	int m;cin >> m;
	for(int i=0 ; i<m ; i++){
		int l , r;cin >> l >> r;l--;r--;
		cout << st->ask(l , r , 1).ans << "\n";
	}
	delete st;
}


#include <iostream>
#include <cstdio>

using namespace std;

/*class FastIO{
public:
    inline FastIO & operator << (const int &a){ printf("%d" , a); return *this; }
    inline FastIO & operator << (const char * const&a){ printf("%s" , a); return *this; }
    inline FastIO & operator >> (int &a){ scanf("%d" , &a); return *this; }
    inline FastIO & operator >> (char * const&a){ scanf("%s" , a); return *this; }
}hir;*/

struct node{
    int ans , op , cl;
    node():
        ans(0) , op(0) , cl(0) {};

};

const int N = 1e6+1 , LG = 20;

node lr[N][LG];
char s[N];
node merg(node *a , node *b){
    node ret;
    int mn = std::min(a->op , b->cl );
    ret.ans = 2*mn + a->ans + b->ans;
    ret.op = a->op + b->op - mn;
    ret.cl = a->cl + b->cl - mn;
    return ret;
}

node get_par(int v , int cnt){
    node ans;
    for(int p=0 ; p<LG ; p++)
        if( cnt & (1<<p) ){
            ans = merg(&lr[v][p] , &ans);
            v = v - (1<<p);
        }
    return ans;
}

int main(){
    node a , b;
    cin >> s;
    int n=0;
    while( s[n] != NULL ) n++;

    for(int i=0 ; i<=n ; i++)
        for(int j=0 ; j<LG ; j++)
            lr[i][j] = node();

    for(int i=0 ; i<=n ; i++){
        if(i == 0) continue;
        node cur;
        if(s[i-1] == '(') cur.op++; else cur.cl++;
        lr[i][0] = cur;
        for(int bit=1 ; bit<LG ; bit++){
            if( i - (1<<(bit-1)) < 0 ) continue;
            lr[i][bit] = merg( &lr[ i - (1<<(bit-1)) ][bit-1] , &lr[i][bit-1] );
        }
    }
    int q;cin >> q;
    for(int i=0 ; i<q ; i++){
        int l ,r;cin >> l >> r;l--;
        cout << get_par(r , r-l).ans << "\n";
    }
}


#include <iostream>
#include <cstdio>

using namespace std;

/*class FastIO{
public:
    inline FastIO & operator << (const int &a){ printf("%d" , a); return *this; }
    inline FastIO & operator << (const char * const&a){ printf("%s" , a); return *this; }
    inline FastIO & operator >> (int &a){ scanf("%d" , &a); return *this; }
    inline FastIO & operator >> (char * const&a){ scanf("%s" , a); return *this; }
}hir;*/

struct node{
    int ans , op , cl;
    node():
        ans(0) , op(0) , cl(0) {};

};

const int N = 1e6+1 , LG = 20;

node lr[N][LG];
char s[N];
node merg(node *a , node *b){
    node ret;
    int mn = std::min(a->op , b->cl );
    ret.ans = 2*mn + a->ans + b->ans;
    ret.op = a->op + b->op - mn;
    ret.cl = a->cl + b->cl - mn;
    return ret;
}

node get_par(int v , int cnt){
    node ans;
    for(int p=0 ; p<LG ; p++)
        if( cnt & (1<<p) ){
            ans = merg(&lr[v][p] , &ans);
            v = v - (1<<p);
        }
    return ans;
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    node a , b;
    cin >> s;
    int n=0;
    while( s[n] != NULL ) n++;

    for(int i=0 ; i<=n ; i++)
        for(int j=0 ; j<LG ; j++)
            lr[i][j] = node();

    for(int i=0 ; i<=n ; i++){
        if(i == 0) continue;
        node cur;
        if(s[i-1] == '(') cur.op++; else cur.cl++;
        lr[i][0] = cur;
        for(int bit=1 ; bit<LG ; bit++){
            if( i - (1<<(bit-1)) < 0 ) continue;
            lr[i][bit] = merg( &lr[ i - (1<<(bit-1)) ][bit-1] , &lr[i][bit-1] );
        }
    }
    int q;cin >> q;
    for(int i=0 ; i<q ; i++){
        int l ,r;cin >> l >> r;l--;
        cout << get_par(r , r-l).ans << "\n";
    }
}


#include <iostream>
#include <conio.h>
using namespace std;

struct node{
    int ans , op , cl;
    node():
        ans(0) , op(0) , cl(0) {};

};

const int N = 1e6+1 , LG = 20;

node lr[N][LG];
int par[N][LG];

node merg(node a , node b){
    node ret;
    int mn = min(a.op , b.cl );
    ret.ans = 2*mn + a.ans + b.ans;
    ret.op = a.op + b.op - mn;
    ret.cl = a.cl + b.cl - mn;
    return ret;
}

node get_par(int v , int cnt){
    node ans;
    for(int p=0 ; p<LG ; p++)
        if( cnt & (1<<p) ){
            ans = merg(lr[v][p] , ans);
            v = par[v][p];
        }
    return ans;
}

int main(){
    node a , b;
    string s;cin >> s;
    int n = s.size();

    for(int i=0 ; i<=n ; i++)
        for(int j=0 ; j<LG ; j++)
            lr[i][j] = node() , par[i][j] = -1;

    for(int i=0 ; i<=n ; i++){
        if(i == 0) continue;
        node cur;
        if(s[i-1] == '(') cur.op++; else cur.cl++;
        lr[i][0] = cur;
        par[i][0] = i-1;
        for(int bit=1 ; bit<LG ; bit++){
            if( par[i][bit-1] < 0 ) continue;
            par[i][bit] = par[ par[i][bit-1] ][bit-1];
            lr[i][bit] = merg( lr[ par[i][bit-1] ][bit-1] , lr[i][bit-1] );
        }
    }
    int q;cin >> q;
    for(int i=0 ; i<q ; i++){
        int l ,r;cin >> l >> r;l--;
        cout << get_par(r , r-l).ans << endl;
    }
}