#include <bits/stdc++.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define ins insert
#define vi vector<int>
const int mxN = 1e9;
const ll mod = 1e9 + 7;

// utility function to form edge between two vertices
// source and dest
void add_edge(vector<pair<int, char>> adj[], int src, int dest, char c)
{
    adj[src].pb(mp(dest, c));
    adj[dest].pb(mp(src, c));
}
bool BFS(vector<pair<int, char>> adj[], int src, int dest, int v,
         int pred[], int dist[])
{
    list<int> queue;
    bool visited[v];
    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.pb(adj[u][i].first);
                if (adj[u][i].first == dest)
                    return true;
            }
        }
    }

    return false;
}
int ShortestDistance(vector<pair<int, char>> adj[], int s,
                     int dest, int v)
{
    int pred[v], dist[v];

    if (BFS(adj, s, dest, v, pred, dist) == false)
        return 0;
    vector<int> path;
    int crawl = dest;
    path.pb(crawl);
    while (pred[crawl] != -1)
    {
        path.pb(pred[crawl]);
        crawl = pred[crawl];
    }
    if (dist[dest] < 3)
        return 0;
    vector<int> c(3, 0);
    for (int i = path.size() - 1; i >= 1; i--)
    {
        for (auto it = adj[path[i]].begin(); it != adj[path[i]].end(); it++)
        {
            int ind = it->first;
            char ch = it->second;
            if (ind == path[i - 1])
            {
                if (ch == 'S')
                    c[0]++;
                else if (ch == 'I')
                    c[1]++;
                else
                    c[2]++;
                //cout << ch << "\n";
            }
        }
        //cout << adj[path[i][i - 1].second];
    }
    if (c[0] == c[1] && c[1] == c[2])
        return 1;
    else
        return 0;
    for (int i = path.size() - 1; i >= 0; i--)
        cout << path[i] + 1 << " ";
    cout << "\n";
}
int main()
{
    int n;
    cin >> n;
    vector<pair<int, char>> adj[n];
    vector<char> branches;
    for (int i = 1; i < n; i++)
    {
        int x, y;
        char c;
        cin >> x >> y >> c;
        x--, y--;
        add_edge(adj, x, y, c);
    }
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            ans += ShortestDistance(adj, i, j, n);
        }
    }
    cout << ans;
    return 0;
}