using ll=longlong; using ull=unsignedlonglong; intread(){ int f=1,x=0;char c=getchar(); while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } ull ans = 0;
using ll=longlong; using ull=unsignedlonglong; using pii=pair<int,int>; #define begend(x) x.begin(),x.end() #define mem0(x) memset(x,0,sizeof(x)) #define YES puts("YES") #define NO puts("NO")
intread(){ int f=1,x=0;char c=getchar(); while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } constint N = 1e5+5; bool vis[N];
vector<pii> v;
intmain(){ int n=read(); for(int i=1;i<=3*n;i++){ int x=read(); if(vis[x]){ v.push_back({i,x}); vis[x]=0; }else{ vis[x]=1; } }
using ll=longlong; using ull=unsignedlonglong; using pii=pair<int,int>; #define begend(x) x.begin(),x.end() #define mem0(x) memset(x,0,sizeof(x)) #define YES puts("YES") #define NO puts("NO")
intread(){ int f=1,x=0;char c=getchar(); while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; }
constint N = 3e5+5;
ll dp[N][2];
intmain(){ int n=read(); for(int i=1;i<=n;i++){ int a=read(), b=read(); if(a==0){ dp[i][0]=max({dp[i-1][0]+b,dp[i-1][1]+b,dp[i-1][0]}); dp[i][1]=dp[i-1][1]; }else{ dp[i][1]=max(dp[i-1][0]+b,dp[i-1][1]); dp[i][0]=dp[i-1][0]; } }
using ll=longlong; using ull=unsignedlonglong; using pii=pair<int,int>; #define begend(x) x.begin(),x.end() #define mem0(x) memset(x,0,sizeof(x)) #define YES puts("YES") #define NO puts("NO")
intread(){ int f=1,x=0;char c=getchar(); while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } constint N = 5e5+5;
using ll=longlong; using ull=unsignedlonglong; using pii=pair<int,int>; #define begend(x) x.begin(),x.end() #define mem0(x) memset(x,0,sizeof(x)) #define YES puts("YES") #define NO puts("NO")
intread(){ int f=1,x=0;char c=getchar(); while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; }
constint N = 1e4+5,M=1e2+5; int t[N*M]; int a[N][M];
using ll=longlong; using ull=unsignedlonglong; using pii=pair<int,int>; #define begend(x) x.begin(),x.end() #define mem0(x) memset(x,0,sizeof(x)) #define YES puts("YES") #define NO puts("NO")
intread(){ int f=1,x=0;char c=getchar(); while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } template <classT> T read(){ T f=1,x=0;char c=getchar(); while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; }
constint N = 1000005; int rt, tot, fa[N], ch[N][2], val[N], cnt[N], sz[N];
voidrotate(int x){ int y = fa[x], z = fa[y], chk = get(x); ch[y][chk] = ch[x][chk ^ 1]; if (ch[x][chk ^ 1]) fa[ch[x][chk ^ 1]] = y; ch[x][chk ^ 1] = y; fa[y] = x; fa[x] = z; if (z) ch[z][y == ch[z][1]] = x; maintain(y); maintain(x); }
voidsplay(int x){ for (int f = fa[x]; f = fa[x], f; rotate(x)) if (fa[f]) rotate(get(x) == get(f) ? f : x); rt = x; }
voidins(int k){ if (!rt) { val[++tot] = k; cnt[tot]++; rt = tot; maintain(rt); return; } int cur = rt, f = 0; while (1) { if (val[cur] == k) { cnt[cur]++; maintain(cur); maintain(f); splay(cur); break; } f = cur; cur = ch[cur][val[cur] < k]; if (!cur) { val[++tot] = k; cnt[tot]++; fa[tot] = f; ch[f][val[f] < k] = tot; maintain(tot); maintain(f); splay(tot); break; } } }
intrk(int k){ int res = 0, cur = rt; while (1) { if (k < val[cur]) { cur = ch[cur][0]; } else { res += sz[ch[cur][0]]; if (k == val[cur]) { splay(cur); return res + 1; } res += cnt[cur]; cur = ch[cur][1]; } } }
intkth(int k){ int cur = rt; while (1) { if (ch[cur][0] && k <= sz[ch[cur][0]]) { cur = ch[cur][0]; } else { k -= cnt[cur] + sz[ch[cur][0]]; if (k <= 0) { splay(cur); return val[cur]; } cur = ch[cur][1]; } } }
intpre(){ int cur = ch[rt][0]; if (!cur) return cur; while (ch[cur][1]) cur = ch[cur][1]; splay(cur); return cur; }
intnxt(){ int cur = ch[rt][1]; if (!cur) return cur; while (ch[cur][0]) cur = ch[cur][0]; splay(cur); return cur; }
voiddel(int k){ rk(k); if (cnt[rt] > 1) { cnt[rt]--; maintain(rt); return; } if (!ch[rt][0] && !ch[rt][1]) { clear(rt); rt = 0; return; } if (!ch[rt][0]) { int cur = rt; rt = ch[rt][1]; fa[rt] = 0; clear(cur); return; } if (!ch[rt][1]) { int cur = rt; rt = ch[rt][0]; fa[rt] = 0; clear(cur); return; } int cur = rt; int x = pre(); fa[ch[cur][1]] = x; ch[x][1] = ch[cur][1]; clear(cur); maintain(rt); } } tree;
ll ans = 0; int maxn = 0; intmain(){ int n=read(),m=read(); for(int i=1;i<=n;i++){ vector<int> v; for(int j=1;j<=m;j++)v.push_back(read()); sort(begend(v)); if(i!=1){ ans += (1+m)*m/2 * (i-1); for(int x:v){ tree.ins(x);maxn=max(maxn,x); ans += tree.rk(maxn)-tree.rk(x); } } elsefor(int x:v){ tree.ins(x);maxn=max(maxn,x); } } printf("%lld\n",ans); return0; }