#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long llong;
typedef pair <llong, int> PLI;
#define x first
#define y second
const int NMAX = 1 << 16;
const int INF = 0x3f3f3f3f;
const int MIN = -NMAX, MAX = INF;
int N;
int A[NMAX], B[NMAX], C[NMAX];
int O[NMAX], NO;
PLI S[NMAX];
void read(void) {
FILE *fin = fopen("inundatii.in", "rt");
int i;
fscanf(fin, " %d", &N);
for (i = 1; i <= N; ++i)
fscanf(fin, " %d %d %d", A+i, B+i, C+i);
fclose(fin);
}
inline int pos(int x) {
return lower_bound(O, O + NO, x) - O + 1;
}
void update(int poz, int x, int y) {
for (;poz <= NO; poz += poz & ~(poz-1))
S[poz].x += x,
S[poz].y += y;
}
PLI query(int poz) {
PLI rez(0, 0);
for (; poz > 0; poz -= poz & ~(poz-1))
rez.x += S[poz].x,
rez.y += S[poz].y;
return rez;
}
llong get(int p, int h) {
int i;
llong rez = 0;
PLI ans, aux;
i = upper_bound(O, O + NO, h-p) - O;
ans = query(i);
rez = (h-p) * ans.y - ans.x;
ans = query(NO);
aux = query(i);
ans.x -= aux.x; ans.y -= aux.y;
rez += ans.x - (h-p) * ans.y;
return rez;
}
int seek(int p, int l, int h) {
// printf("l=%d h=%d\n", l, h);
if (l == h) return l;
if (h - l <= 4) {
int i, poz = l, t, aux;
t = get(p, l);
for (i = l+1; i <= h; ++i)
if ((aux = get(p, i)) < t)
t = aux, poz = i;
return poz;
}
int l3, h3;
l3 = ((llong)l * 2 + h) / 3;
h3 = ((llong)l + 2 * h) / 3;
if (get(p, l3) > get(p, h3))
return seek(p, l3, h);
else
return seek(p, l, h3);
}
llong solve(int A[]) {
int i, j, lh, h;
llong rez = 0;
for (i = 1; i <= N; ++i)
O[i-1] = A[i] - i;
sort(O, O + N);
NO = unique(O, O + N) - O;
for (i = 1; i <= N; ++i) {
j = pos(A[i]-i);
update(j, A[i]-i, 1);
}
lh = MIN;
for (i = 1; i <= N; ++i) {
h = seek(i, lh, INF);
// printf("%d\n", h);
rez += abs(h - A[i]);
lh = h + 1;
j = pos(A[i]-i);
update(j, -A[i]+i, -1);
}
// printf("rez=%lld\n", rez);
return rez;
}
void write(void) {
FILE *fout = fopen("inundatii.out", "wt");
llong rez;
rez = solve(A) + solve(B) + solve(C);
fprintf(fout, "%lld\n", rez);
fclose(fout);
}
int main(void) {
read();
write();
return 0;
}