#include using namespace std; using ll = long long; struct unionfind { vector dat; unionfind(int n) : dat(n, -1) {} int find(int x) { if (dat[x] < 0) return x; return dat[x] = find(dat[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (dat[x] > dat[y]) swap(x, y); dat[x] += dat[y]; dat[y] = x; } int size(int x) { return -dat[find(x)]; } }; int lcp(string s, string t) { int k = 0; while (k < s.size() and k < t.size() and s[k] == t[k]) k++; return k; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin >> N; vector S(N); for (int i = 0; i < N; i++) { cin >> S[i]; } vector> pos(200001); for (int i = 0; i + 1 < N; i++) { pos[lcp(S[i], S[i + 1])].push_back(i); } ll ans = 0; ll ima = 0; unionfind uf(N); auto f = [&](ll n) { return n * (n + 1) * (n + 2) / 6; }; for (int i = 200000; i >= 1; i--) { for (int j : pos[i]) { ima -= f(uf.size(j)); ima -= f(uf.size(j + 1)); uf.unite(j, j + 1); ima += f(uf.size(j)); } ans += ima; } for (int i = 0; i < N; i++) { ans += S[i].size(); } cout << ans << endl; }