#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"
using namespace std;
typedef long long int LL;
const int N = 1e6 + 7;
const int T = (1 << 20) + 7;
int n, id;
int pref[N];
int tree[T + T + 7];
int ask(int from, int to){
from += T;
to += T;
int ret = 0;
while(from < to){
if(from & 1)
ret += tree[from];
if(!(to & 1))
ret += tree[to];
from = (from + 1) >> 1;
to = (to - 1) >> 1;
}
if(from == to)
ret += tree[to];
return ret;
}
void update(int p){
p += T;
while(p){
tree[p]++;
p >>= 1;
}
}
int main(){
id = MyNodeId();
n = GetN();
if(n < 100){
if(id != 0)
return 0;
int ans = 0;
for(int i = 0; i < n; ++i)
for(int j = i + 1; j < n; ++j)
ans += GetElement(i) > GetElement(j);
printf("%d\n", ans);
return 0;
}
int sz = n / 100;
int from = sz * id, to = sz * (id + 1);
if(id == 99)
to = max(to, n);
for(int i = 0; i < from; ++i)
pref[GetElement(i)]++;
for(int i = 1; i < N; ++i)
pref[i] += pref[i - 1];
LL ans = 0;
for(int i = from; i < to; ++i){
int v = GetElement(i);
ans += pref[N - 1] - pref[v];
ans += ask(v + 1, N - 1);
update(v);
}
if(id > 0){
PutLL(0, ans);
Send(0);
return 0;
}
for(int i = 1; i < 100; ++i){
Receive(i);
ans += GetLL(i);
}
printf("%lld\n", ans);
return 0;
}