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

int solution = 0;

bool BFS(vector<vector<pair<int, char>>> adj, int src, int v, int pred[], int dist[])
{
    list<int> queue;
    bool visited[v]; // poseceni
    for (int i = 0; i < v; i++) {
        visited[i] = false;
        dist[i] = INT_MAX;
        pred[i] = -1;
    }

    visited[src] = true;
    dist[src] = 0;
    queue.push_back(src);

    while (!queue.empty()) {
        int u = queue.front();
        queue.pop_front();
        for (int i = 0; i < adj[u].size(); i++) {
            if (visited[adj[u][i].first] == false) {
                visited[adj[u][i].first] = true;
                dist[adj[u][i].first] = dist[u] + 1;
                pred[adj[u][i].first] = u;
                queue.push_back(adj[u][i].first);
            }
        }
    }

    return true;
}

void proveriSIO(vector<vector<pair<int, char>>> adj, int s, int v)
{

    int pred[v], dist[v];

    if (BFS(adj, s, v, pred, dist) == false) return;

    for (int j = s+1; j < v; ++j) {
        vector<int> path;
        int crawl = j;
        path.push_back(crawl);
        while (pred[crawl] != -1) {
            path.push_back(pred[crawl]);
            crawl = pred[crawl];
        }
        // proveri pojavljivanja S I O (put mora da bude veci ili jednak sa tri)
        if (dist[j] >= 3) {
            int u = path[path.size()-1];
            int ps = 0, pi = 0, po = 0;
            for (int i = path.size() - 2; i >= 0; i--) {
                int v = path[i];
                for (auto x : adj[u])
                    if (x.first == v) {
                        if (x.second == 'S') ++ps;
                        else if (x.second == 'I') ++pi;
                        else if (x.second == 'O') ++po;
                        break;
                    }
                u = v;
            }

            if (ps == pi && ps != 0)
                if (ps == po) ++solution;
        }
    }
}

int main()
{
    int n; scanf("%d", &n);
    vector<vector<pair<int, char>>> g(n);

    for (int i = 0; i < n-1; ++i) {
        int u, v;
        char slovo;
        scanf("%d %d %c", &u, &v, &slovo); --u; --v;
        g[u].push_back({v, slovo});
        g[v].push_back({u, slovo});
    }

    for (int i = 0; i < n; ++i)
        proveriSIO(g, i, n);

    printf("%d", solution);

    return 0;
}