#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937/*_64*/ rng(chrono::steady_clock::now().time_since_epoch().count());
#define printArr(arr, n) cerr << #arr << " = ["; for(int i=0; i<(n); i++){if (i) cerr << ", "; cerr << arr[i];} cerr << "]\n"
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
void _print() {cerr << "]\n";}
template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
vector<int> place[26];
vector<int> pos[26];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	string s;
	cin >> s;
	int n = SZ(s);
	for (int q = 0; q < n; q++) {
		pos[s[q]-'a'].push_back(q);
	}
	for (int q = n-2; q >= 0; q--) {
		if (s[q] == s[q+1]) {
			place[s[q]-'a'].push_back(q);
		}
	}
	for (int q = 0; q < 26; q++) {
		place[q].push_back(n);
		sort(place[q].begin(), place[q].end());
	}
	ll ans = 0;
	for (int q = 0; q < 26; q++) {
		for (int w = 0; w < SZ(place[q]); w++) {
			if (w == SZ(place[q])-1) {
				continue;
			} else {
				if (place[q][w]+1==place[q][w+1]) continue;
				bool between = false;
				int last = n;
				for (int e = 0; e < 26; e++) {
					if (q == e) continue;
					auto it = upper_bound(place[e].begin(), place[e].end(), place[q][w]);
					if (*it < place[q][w+1]) {
						between = true;
						last = min(last, *it);
					}
				}
				if (between) {
					ll x = n-2-place[q][w];
					ll lo = upper_bound(pos[q].begin(), pos[q].end(), place[q][w]+1)-pos[q].begin();
					ll hi = lower_bound(pos[q].begin(), pos[q].end(), last)-pos[q].begin();
					hi--;
					ll dif = max(0LL,hi-lo+1);
					x -= dif;
					ans += x;
					continue;
				}
				ll x = place[q][w+1]-place[q][w]-2;
				ll lo = upper_bound(pos[q].begin(), pos[q].end(), place[q][w]+1)-pos[q].begin();
				ll hi = lower_bound(pos[q].begin(), pos[q].end(), place[q][w+1]) - pos[q].begin();
				hi--;
				ll dif = max(0LL,hi-lo+1);
				x -= dif;
				ans += x;
			}
		}
	}
	cout << ans << "\n";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(ll x=pow(10,7)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

struct UnionFind {
  vector<ll> par, s, e; 
  UnionFind(ll N) : par(N), s(N), e(N) { rep(i,N) { par[i] = i; s[i] = 1; e[i] = 0; } }
  ll root(ll x) { return par[x]==x ? x : par[x] = root(par[x]); }
  ll size(ll x) { return par[x]==x ? s[x] : s[x] = size(root(x)); }
  void unite(ll x, ll y) { 
    ll rx=root(x), ry=root(y); 
    if (rx!=ry) { s[rx] += s[ry]; e[rx] += e[ry]+1; par[ry] = rx; }
    else e[rx]++;
  }
  bool same(ll x, ll y) {  ll rx=root(x), ry=root(y); return rx==ry; }
};

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

int main() {
  ll N; cin >> N;
  ll a[N], b[N]; rep(i,N) cin >> a[i] >> b[i];

  ll MA = 400010;
  UnionFind uf(MA);
  rep(i,N) uf.unite(a[i],b[i]);
  ll result = 0; rep(i,MA) if (uf.root(i)==i) result += min(uf.s[i],uf.e[i]);
  cout << result << endl;
  return 0;
}