/*
Author: Nguyen Tran Tuan Tu - A2-K68 - Ngo Quyen High school
Training VOI 2026
*/
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #pragma GCC optimize("O2", "unroll-loops", "Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimize("unroll-loops,02,no-stack-protector")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// input/output
#define fastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define el cout << '\n'
#define execute cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << "s"
//data type
#define int128 __uint128_t
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define piv pair<int, vector<int>>
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
//STL
#define sz(x) (int)(x).size()
#define for1(i,l,r) for(auto i = l; i <= r; i++)
#define for2(i,r,l) for(auto i = r; i >= l; i--)
#define for3(i,l,r) for(auto i = l; i < r; i++)
#define for4(i,r,l) for(auto i = r; i > l; i--)
#define forec(i,a) for(auto i : a)
#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
//bitmask
#define bitcnt(n) __builtin_popcount(n)
#define mask(i) (1 << (i))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_on(n, i) ((n) | mask(i))
#define set_off(n, i) ((n) & ~mask(i))
#define all_subset(mask) (1 << mask)
#define is_bit(pos, mask) ((mask >> pos) & 1)
#define all(x) (x).begin(), (x).end()
//constant
#define N 1000005
#define MOD 998244353
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
const int mod = 1e9 + 7;
const int d4x[4] = {-1, 0, 1, 0}, d4y[4] = {0, 1, 0, -1};
const int d8x[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8y[8] = {0, 1, 1, 1, 0, -1, -1, -1};
//using namespace chrono;
//using namespace __gnu_pbds;
using namespace std;
/*----------------------------------------------------------------------------------------------------
NTTT_NgoQuyenHighSchool_A2_K68 */
int n, dp[1000][1000];
string s;
void read(){
cin >> n >> s;
}
/*----------------------------------------------------------------------------------------------------*/
void solve(){
for(int i = 0; i < n; i++){
dp[i][i] = 1;
}
for(int l = 2; l <= n; l++) {
for (int i = 0; i + l - 1 < n; i++) {
int j = i + l - 1;
if(s[i] == s[j]){
dp[i][j] = dp[i][j - 1]+1;
}
for(int k = i; k < j; k++){
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
}
}
}
cout << dp[0][n - 1] << endl;
}
/*----------------------------------------------------------------------------------------------------*/
int main(){
#define NAME "TUANTU"
if(fopen(NAME".INP", "r")){
freopen(NAME".INP", "r", stdin);
freopen(NAME".OUT", "w", stdout);
}
fastIO;
int t = 1;
while(t--){
read();
solve();
}
return execute, 0;
}
LyoKICAgICAgICAgQXV0aG9yOiBOZ3V5ZW4gVHJhbiBUdWFuIFR1IC0gQTItSzY4IC0gTmdvIFF1eWVuIEhpZ2ggc2Nob29sCiAgICAgICAgIFRyYWluaW5nIFZPSSAyMDI2CiovCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgovLyAjaW5jbHVkZTxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KLy8gI2luY2x1ZGU8ZXh0L3BiX2RzL3RyZWVfcG9saWN5LmhwcD4KLy8gI3ByYWdtYSBHQ0Mgb3B0aW1pemUoIk8yIiwgInVucm9sbC1sb29wcyIsICJPZmFzdCIpCi8vICNwcmFnbWEgR0NDIHRhcmdldCgiYXZ4LGF2eDIsZm1hIikKLy8gI3ByYWdtYSBHQ0Mgb3B0aW1pemUoInVucm9sbC1sb29wcywwMixuby1zdGFjay1wcm90ZWN0b3IiKQovLyAjcHJhZ21hIEdDQyBvcHRpbWl6ZSgiTzMsdW5yb2xsLWxvb3BzIikKLy8gI3ByYWdtYSBHQ0Mgd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