#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;

void add_edge(vector<pair<int, char>> adj[], int U, int V, char c)
{
    adj[U].pb(mp(V, c));
    adj[V].pb(mp(U, c));
}
bool BFS(vector<pair<int, char>> adj[], int U, int V, int n,
         int prev[], int dist[])
{
    list<int> queue;
    bool visited[n];
    for (int i = 0; i < n; i++)
    {
        visited[i] = false;
        dist[i] = INT_MAX;
        prev[i] = -1;
    }
    visited[U] = true;
    dist[U] = 0;
    queue.pb(U);
    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;
                prev[adj[u][i].first] = u;
                queue.pb(adj[u][i].first);
                if (adj[u][i].first == V)
                    return true;
            }
        }
    }

    return false;
}
int Distance(vector<pair<int, char>> adj[], int U,
             int V, int n)
{
    int prev[n], dist[n];
    if (BFS(adj, U, V, n, prev, dist) == false)
        return 0;
    vector<int> path;
    int curr = V;
    path.pb(curr);
    while (prev[curr] != -1)
    {
        path.pb(prev[curr]);
        curr = prev[curr];
    }
    if (dist[V] < 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];
    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 += Distance(adj, i, j, n);
        }
    }
    cout << ans;
    return 0;
}