#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
char s[maxn];
int n,sum[maxn][30];
long long ans;
int book;
char ch;

int main() {
	scanf("%s",s+1);
	n=strlen(s+1);
	book=n+1; ch='a'-1;
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=26;j++) sum[i][j]=sum[i-1][j];
		sum[i][(s[i]-'a'+1)]++;
	}
	
	for(int i=n-2;i>=1;i--) {
		if(s[i]==s[i+1] && s[i+1]!=s[i+2]) {
			if(ch==s[i]) ans+=(book-1)-(i+1);
			else ans+=n-(i+1);
			ans-=sum[(book-1)][(s[i]-'a'+1)]-sum[i+1][(s[i]-'a'+1)];
			book=i,ch=s[i];
		}
	}
	printf("%lld",ans);
	return 0;
}
#include <bits/stdc++.h>
#include <variant>

#define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) begin(x),end(x)
#ifdef ENV_LOCAL
#define dump if (1) cerr
#else
#define dump if (0) cerr
#endif
 
using namespace std;
using namespace std::string_literals;
 
using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;

pair<i32,bool> solve(const vector<vi32>& g, i32 i) {
  bool flip = true;
  vector<pair<i32,bool>> children;
  for (auto&& to : g[i]) {
    auto [sc, fl] = solve(g, to);
    if (fl) flip = !flip;
    children.emplace_back(-sc, fl);
  }
  dump<<i<<endl;
  sort(all(children), [](auto&& lhs, auto&& rhs) {
      auto [ls, lf] = lhs;
      auto [rs, rf] = rhs;
      if (ls >= 0 && rs < 0) return true;
      else if (ls < 0 && rs >= 0) return false;
      if (ls >= 0) {
        if (lf == rf) {
          return ls > rs;
        } else {
          return rf;
        }
      } else {
        if (lf == rf) {
          return ls > rs;
        } else {
          return lf;
        }
      }
  });
  i32 score = 1;
  bool current = false;
  for (auto&& [sc, fl] : children) {
    dump<<sc<<" "<<fl<<endl;
    if (current) {
      score += sc;
    } else {
      score -= sc;
    }
    if (fl) current = !current;
  }
  return make_pair(score, flip);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  i32 n;
  cin>>n;
  vector<vi32> g(n);
  rep2(i,1,n) {
    i32 p;
    cin>>p;
    --p;
    g[p].push_back(i);
  }
  auto [score, flip] = solve(g, 0);
  auto ans = (n + score) / 2;
  cout<<ans<<endl;
  return 0;
}
