#include "message.h"
#include "teatr.h"
#include <iostream>
using namespace std;
const int CHUNK = 1000000;
int count[CHUNK] = { 0 };
int main() {
int n = GetN();
int id = MyNodeId();
long long issues = 0;
if (id * CHUNK < n) {
int start = id * CHUNK;
for (int i = start; i < start + CHUNK && i < n; i++) {
int element = GetElement(i);
count[element - 1]++;
for (int j = element; j < 5; j++) {
issues += count[j];
}
}
}
if (id != 0) {
PutLL(0, issues);
PutInt(0, count[0]);
PutInt(0, count[1]);
PutInt(0, count[2]);
PutInt(0, count[3]);
PutInt(0, count[4]);
Send(0);
} else {
for (int i = 1; i < NumberOfNodes(); i++) {
Receive(i);
issues += GetLL(i);
int c0 = GetInt(i);
issues += c0 * (count[1] + count[2] + count[3] + count[4]);
count[0] += c0;
int c1 = GetInt(i);
issues += c1 * (count[2] + count[3] + count[4]);
count[1] += c1;
int c2 = GetInt(i);
issues += c2 * (count[3] + count[4]);
count[2] += c2;
int c3 = GetInt(i);
issues += c3 * count[4];
count[3] += c3;
int c4 = GetInt(i);
count[4] += c4;
}
cout << issues;
}
return 0;
}