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

#define rep(i, n) for(int i = 0, i##_len=(n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())

void _cin() {} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail) { cin >> head; _cin(forward<Tail>(tail)...); }
#define cin(Type, ...) Type __VA_ARGS__; _cin(__VA_ARGS__)
#define cinv(Type, xs, n) vector<Type> xs(n); rep(i, n) cin >> xs[i]
#define cinv2(Type, xs, ys, n) vector<Type> xs(n), ys(n); rep(i, n) cin >> xs[i] >> ys[i]
#define cinv3(Type, xs, ys, zs, n) vector<Type> xs(n), ys(n), zs(n); rep(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define cinvv(Type, xs, h, w) vector<vector<Type>> xs(h, vector<Type>(w)); rep(i, h) rep(j, w) cin >> xs[i][j]

void print() { cout << endl; }
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; print(forward<Tail>(tail)...); }
template <class Type> void print(vector<Type> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }
template <class Type> void print(vector<vector<Type>> &df) { for (auto& vec : df) { print(vec); } }

void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void yesno(bool b) { cout << (b ? "yes" : "no") << endl; }
template<class Integer>bool chmax(Integer &a, const Integer &b) { if (a < b) { a = b; return 1; } return 0; }
template<class Integer>bool chmin(Integer &a, const Integer &b) { if (b < a) { a = b; return 1; } return 0; }

using ll = long long;
using P = pair<int, int>;

void Main() {
	cin(string, s);
	int n = len(s);
	ll ans = 0;
	char c;
	bool ok = false;
	int j = 0;
	vector<int> v;
	rep(i, n-1) {
		if (ok) {
			if (s[i] == s[i + 1] && (s[i] != c)) {
				c = s[i];
				v.emplace_back(1);
				j++;
				continue;
			}
			if (s[i] != c) {
				ans++;
			}
			v[j]++;
		}
		else if (s[i] == s[i + 1]){
			ok = true;
			c = s[i];
			v.emplace_back(1);
		}
	}
	if (!ok) {
		print(0);
		return;
	}
	if (s[n - 1] != c) {
		ans++;
	}
	v[j]++;
	reverse(all(v));
	rep(i, len(v) - 1) {
		ans += v[i];
		v[i + 1] += v[i];
	}
	print(ans);
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	//cout << fixed << setprecision(16);
	//int t; cin >> t; while (t--)
	Main();
}
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn=2000;
const int t[5][2]={{0, 0}, {0, 0}, {0, -1}, {+1, 0}, {+1, -1}};
int n, a[maxn+1][2];

int main() {
	scanf("%d", &n);
	int cnt=0;
	for (int i=1; i<=n; i++) {
		int opt;
		scanf("%d %d %d", &opt, a[i], a[i]+1);
		a[i][0]=a[i][0]*2+t[opt][0], a[i][1]=a[i][1]*2+t[opt][1];
		for (int j=1; j<i; j++) {
			if (min(a[i][1], a[j][1])>=max(a[i][0], a[j][0])) cnt++;
		}
	}
	printf("%d\n", cnt);
	return 0;
}
