function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }
        var m = parseInt((a[0] + 1) / 2)
        for(var i = 0; i < m; i++) {
                var l = 0, r = a[1] - 1;
                while(l <= r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        obj[x] = (obj[x] ? obj[x] + 1 : 1);
                        obj[y] = (obj[y] ? obj[y] + 1 : 1);
                        obj[z] = (obj[z] ? obj[z] + 1 : 1);
                        obj[w] = (obj[w] ? obj[w] + 1 : 1);
                        if(obj[x] == 1 && obj[y] == 1 && obj[z] == 1 && obj[w] == 1) {
                                toSort.sort(cmp);
                                add = Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);
                        }else {
                                if(obj[x] != 1) {
                                        add = Math.abs(y - x) + Math.abs(z - x) + Math.abs(w - x);
                                }else if(obj[y] != 1) {
                                        add = Math.abs(x - y) + Math.abs(z - y) + Math.abs(w - y);
                                }else {
                                        add = Math.abs(x - z) + Math.abs(y - z) + Math.abs(w - z);
                                }
                        }

                        if(l == r || (i + 1 == m && a[0] % 2)) ans += parseInt(add / 2)
                        else ans += add
                        l++, r--;
                }
        }
        print(ans);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? -1 : 1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }
        var m = parseInt((a[0] + 1) / 2)
        for(var i = 0; i < m; i++) {
                var l = 0, r = a[1] - 1;
                while(l <= r) {

                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var sr = [x, y, z, w];
                        sr.sort(mp)
                        add = Math.min(Math.abs(sr[0] - sr[1]) + Math.abs(sr[2] - sr[1]) + Math.abs(sr[3] - sr[1]),
                                Math.abs(sr[0] - sr[2]) + Math.abs(sr[1] - sr[2]) + Math.abs(sr[3] - sr[2])
                                ,Math.abs(sr[1] - sr[0]) + Math.abs(sr[2] - sr[0]) + Math.abs(sr[3] - sr[0])
                                ,Math.abs(sr[0] - sr[3]) + Math.abs(sr[1] - sr[3]) + Math.abs(sr[2] - sr[3]));

                        if(l == r || (i + 1 == m && a[0] % 2)) ans += parseInt(add / 2)
                        else ans += add
                        l++, r--;
                }
        }
        print(ans);
}

'use strict'
function solv() {
    var ip = readline().split(' ').map(x => parseInt(x));
    var x = ip[0];
    var y = ip[1];
    var grid = new Array(x);
    for (var n = 0; n < x; n++) {
        grid[n] = readline().split(' ').map(x => parseInt(x));
    }
    var res = 0;
    for (var n = 0; n < x; n++) {
        for (var k = 0; k < y; k++) {
            var a = grid[n][k];
            var b = grid[x - 1 - n][k];
            var c = grid[n][y - 1 - k];
            var d = grid[x - 1 - n][y - 1 - k];
            var tm = [a, b, c, d];
            tm.sort((x, y) => { return x > y });
            var md = tm[3] - tm[0] + tm[2] - tm[1]
            res += md
            /**/
        }
    }

    print(Math.floor(res / 4))
}

var tc = parseInt(readline())
while (tc--) solv()

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const [_row, _col] = readLine()
      .split(" ")
      .map((n) => parseInt(n));
    const arr = [];
    for (let i = 0; i < _row; i++) {
      const r = readLine()
        .split(" ")
        .map((n) => parseInt(n));
      arr.push(r);
    }

    let count = 0;

    for (let row = 0; row < _row; row++) {
      for (let col = 0; col < _col / 2; col++) {
        let nums = [arr[row][col], arr[_row - 1 - row][col], arr[row][_col - 1 - col]];
        nums.sort((a, b) => a - b);
        const target = nums[1];
        count += Math.abs(target - nums[0]) + Math.abs(target - nums[1]) + Math.abs(target - nums[2]);
        [arr[row][col], arr[_row - 1 - row][col], arr[row][_col - 1 - col]] = [target, target, target];
      }
    }
    console.log(count);
  }
}

function For(a, b, fn) {
  if (!fn) return For(0, a - 1, b);
  let res;
  for (let i = a; i <= b; i++) {
    if ((res = fn(i)) != null) return res;
  }
}
function ForR(a, b, fn) {
  if (!fn) return ForR(a - 1, 0, b);
  let res;
  for (let i = a; i >= b; i--) {
    if ((res = fn(i)) != null) return res;
  }
}

function Arr(a, b, fn) {
  if (typeof (b) == 'function') return Arr(0, a - 1, b);
  if (b < a) return [];
  let arr = Array(b - a + 1)
  for (let i = a; i <= b; i++) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn ? fn(v) : v).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.set = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}
Array.prototype.for = function (a, b, fn) {
  let res
  for (let i = a; i <= b; i++) {
    if ((res = fn(this[i], i, this)) != null) return res;
  }
}
Array.prototype.forR = function (a, b, fn) {
  let res
  for (let i = a; i >= b; i--) {
    if ((res = fn(this[i], i, this)) != null) return res;
  }
}
Array.prototype.bin = function (comp, a, b) {
  let mid;
  while (a < b) {
    mid = (a + b) / 2 | 0;
    if (typeof comp == 'function' ? comp(this[mid]) : this[mid] < comp) a = mid + 1;
    else b = mid;
  }
  return a;
}
Array.prototype.cut = function (fn, a, b) {
  let arr = [];
  let sub = [];
  a = a || 0;
  b = b || (this.length - 1);
  for (let i = a; i <= b; i++) {
    sub.push(this[i])
    if (fn(this[i], i, this)) {
      arr.push(sub);
      sub = [];
    }
  }
  return arr;
}

function gcd(a, b) {
  while (b) [a, b] = [b, a % b];
  return a;
}
function mul(a, b, m) {
  return ((a >> 16) * b % m * 65536 + (a & 65535) * b) % m
}
function pow(a, n, m) {
  let r = 1;
  while (n) n & 1 ? (r = mul(r, a, m), n--) : [a, n] = [mul(a, a, m), n >> 1]
  return r;
}
let MOD = 998244353
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = a / b | 0, a -= t * b, u -= t * v, t = a, a = b, b = t, t = u, u = v);
  return (u + MOD) % MOD;
}

// let gt = Arr(n, (i, a) => i == 0 ? 1 : mul(a[i - 1], i, MOD));
// let rgt = Arr(n, (i, a) => i <= 1 ? 1 : MOD - mul((MOD / i | 0), a[998244353 % i], MOD));
// For(2, n - 1, i => { rgt[i] = mul(rgt[i], rgt[i - 1], MOD) });
// let nCk = (n, k) => mul(mul(gt[n], rgt[k], MOD), rgt[n - k], MOD);

// for (i = pos; i <= n; i += i & -i) fen[i]++; // start from index 1
// for (i = pos; i; i -= i & -i) ans += fen[i];

// let p = [2];
// for (let i = 3; i < 1e5; i += 2) {
//   let j, x = Math.sqrt(i);
//   for (j = 0; p[j] <= x; j++) if (i % p[j] == 0) break;
//   if (i % p[j] != 0) p.push(i);
// }

// let root = x => par[x] < 0 ? x : (par[x] = root(par[x]));
// let dsu = (x, y) => {
//   if ((x = root(x)) == (y = root(y))) return;
//   if (par[y] < par[x]) [x, y] = [y, x];
//   par[x] += par[y];
//   par[y] = x;
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let t = $()
  while (t--) {
    let [n, m] = $(2), res = 0
    let a = Arr(n, i => $(m))
    // log(a)
    For((n + 1) / 2 | 0, i => {
      For((m + 1) / 2 | 0, j => {
        let a1 = a[i][j], a2 = a[n - 1 - i][j], a3 = a[i][m - 1 - j], a4 = a[n - 1 - i][m - 1 - j];
        let s = min(
          abs(a2-a1)+abs(a3-a1)+abs(a4-a1),
          abs(a1-a2)+abs(a3-a2)+abs(a4-a2),
          abs(a1-a3)+abs(a2-a3)+abs(a4-a3),
          abs(a1-a4)+abs(a2-a4)+abs(a3-a4),
        )
        if (i == n - 1 - i) s /= 2;
        if (j == m - 1 - j) s /= 2;
        res += s;
      })
    })
    log(res)
  }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    B();
});

function readline() {
    return inputString[currentLine++];
}

function pi(a) {
    return parseInt(a);
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = pi(a[i]);

    return a;
}

function B(){
    let t = pi(readline());
    while(t > 0){
        t--;
        let [n,m] = ti(readline().split(' '));
        let mat = new Array(n);
        for(let i = 0; i < n; i++){
            mat[i] = ti(readline().split(' '));
        }

        let count = 0;
        for(let i = 0; i < n/2; i++){
            for(let j = 0; j < m/2; j++){
                let a = [];
                if(i !== n-i-1 && j !== m-j-1){
                    a = [mat[i][j], mat[i][m-j-1], mat[n-i-1][j], mat[n-i-1][m-j-1]];;
                }else{
                    a = [mat[i][j], mat[n-i-1][m-j-1]];
                }
                a.sort((a,b) => a-b);
                if(a.length === 4)
                    count += Math.abs(a[0]-a[1]) + Math.abs(a[1]-a[2]) + Math.abs(a[1]-a[3]);
                else
                    count += Math.abs(a[0]-a[1]);
            }
        }

        console.log(count);
    }
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a, b = readline().split(' ').map(mp);
        var arr = [];
        for(var i = 0; i < a; a++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }

        print(arr);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());
n = 1
while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }

        for(var i = 0; i < a[0] / 2; i++) {
                var l = 0, r = a[1] - 1;
                while(l < r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        obj[x] = (obj[x] ? obj[x] + 1 : 1);
                        obj[y] = (obj[y] ? obj[y] + 1 : 1);
                        obj[z] = (obj[z] ? obj[z] + 1 : 1);
                        obj[w] = (obj[w] ? obj[w] + 1 : 1);
                        if(obj[x] == 1 && obj[y] == 1 && obj[z] == 1 && obj[w] == 1) {
                                toSort.sort(cmp);
                                ans += Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);
                        }else {
                                if(obj[x] != 1) {
                                        ans += Math.abs(y - x) + Math.abs(z - x) + Math.abs(w - x);
                                }else if(obj[y] != 1) {
                                        ans += Math.abs(x - y) + Math.abs(z - y) + Math.abs(w - y);
                                }else {
                                        ans += Math.abs(x - z) + Math.abs(y - z) + Math.abs(w - z);
                                }
                        }
                        l++, r--;
                        print(ans)
                }
        }
        print(ans); // odd rows cols not handel yet xD
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line)
        }

        print(arr[0]);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());
n = 1
while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }

        for(var i = 0; i < a[0]; i++) {
                var l = 0, r = a[1] - 1;
                while(l < r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        obj[x] = (obj[x] ? obj[x] + 1 : 1);
                        obj[y] = (obj[y] ? obj[y] + 1 : 1);
                        obj[z] = (obj[z] ? obj[z] + 1 : 1);
                        obj[w] = (obj[w] ? obj[w] + 1 : 1);
                        if(obj[x] == 1 && obj[y] == 1 && obj[z] == 1 && obj[w] == 1) {
                                toSort.sort(cmp);
                                ans += Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);
                        }else {
                                if(obj[x] != 1) {
                                        ans += Math.abs(y - x) + Math.abs(z - x) + Math.abs(w - x);
                                }else if(obj[y] != 1) {
                                        ans += Math.abs(x - y) + Math.abs(z - y) + Math.abs(w - y);
                                }else {
                                        ans += Math.abs(x - z) + Math.abs(y - z) + Math.abs(w - z);
                                }
                        }
                        l++, r--;
                        print(ans)
                }
        }
        print(ans); // odd rows cols not handel yet xD
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line)
        }

        print(arr);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }
        var m = parseInt((a[0] + 1) / 2)
        for(var i = 0; i < m; i++) {
                var l = 0, r = a[1] - 1;
                while(l <= r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        obj[x] = (obj[x] ? obj[x] + 1 : 1);
                        obj[y] = (obj[y] ? obj[y] + 1 : 1);
                        obj[z] = (obj[z] ? obj[z] + 1 : 1);
                        obj[w] = (obj[w] ? obj[w] + 1 : 1);
                        if(obj[x] == 1 && obj[y] == 1 && obj[z] == 1 && obj[w] == 1) {
                                toSort.sort(cmp);
                                ans += Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);
                        }else {
                                if(obj[x] != 1) {
                                        ans += Math.abs(y - x) + Math.abs(z - x) + Math.abs(w - x);
                                }else if(obj[y] != 1) {
                                        ans += Math.abs(x - y) + Math.abs(z - y) + Math.abs(w - y);
                                }else {
                                        ans += Math.abs(x - z) + Math.abs(y - z) + Math.abs(w - z);
                                }
                        }
                        l++, r--;
                }
        }
        print(ans);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a, b = readline().split(' ').map(mp);
        var arr = [];
        print(a, b)
        for(var i = 0; i < a; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line[0]);
        }

        print(arr);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }

        for(var i = 0; i < a[0]; i++) {
                var l = 0, r = a[1] - 1;
                while(l < r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        obj[x] = (obj[x] ? obj[x] + 1 : 1);
                        obj[y] = (obj[y] ? obj[y] + 1 : 1);
                        obj[z] = (obj[z] ? obj[z] + 1 : 1);
                        obj[w] = (obj[w] ? obj[w] + 1 : 1);
                        if(obj[x] == 1 && obj[y] == 1 && obj[z] == 1 && obj[w] == 1) {
                                toSort.sort(cmp);
                                ans += Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);
                        }else {
                                if(obj[x] != 1) {
                                        ans += Math.abs(y - x) + Math.abs(z - x) + Math.abs(w - x);
                                }else if(obj[y] != 1) {
                                        ans += Math.abs(x - y) + Math.abs(z - y) + Math.abs(w - y);
                                }else {
                                        ans += Math.abs(x - z) + Math.abs(y - z) + Math.abs(w - z);
                                }
                        }
                        l++, r--;
                        print(ans); // odd rows cols not handel yet xD
                }
        }
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a, b = readline().split(' ').map(mp);
        var arr = [];
        print(n)
        for(var i = 0; i < a; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line[0]);
        }

        print(arr);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());
n = 1
while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }

        for(var i = 0; i < (a[0] + 1) / 2; i++) {
                print(i)
                var l = 0, r = a[1] - 1;
                while(l <= r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        obj[x] = (obj[x] ? obj[x] + 1 : 1);
                        obj[y] = (obj[y] ? obj[y] + 1 : 1);
                        obj[z] = (obj[z] ? obj[z] + 1 : 1);
                        obj[w] = (obj[w] ? obj[w] + 1 : 1);
                        if(obj[x] == 1 && obj[y] == 1 && obj[z] == 1 && obj[w] == 1) {
                                toSort.sort(cmp);
                                ans += Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);
                        }else {
                                if(obj[x] != 1) {
                                        ans += Math.abs(y - x) + Math.abs(z - x) + Math.abs(w - x);
                                }else if(obj[y] != 1) {
                                        ans += Math.abs(x - y) + Math.abs(z - y) + Math.abs(w - y);
                                }else {
                                        ans += Math.abs(x - z) + Math.abs(y - z) + Math.abs(w - z);
                                }
                        }
                        l++, r--;
                }
        }
        print(ans);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }

        for(var i = 0; i < a[0]; i++) {
                var l = 0, r = a[1] - 1;
                while(l < r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        obj[x] = (obj[x] ? obj[x] + 1 : 1);
                        obj[y] = (obj[y] ? obj[y] + 1 : 1);
                        obj[z] = (obj[z] ? obj[z] + 1 : 1);
                        obj[w] = (obj[w] ? obj[w] + 1 : 1);
                        if(obj[x] == 1 && obj[y] == 1 && obj[z] == 1 && obj[w] == 1) {
                                toSort.sort(cmp);
                                ans += Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);
                        }else {
                                if(obj[x] != 1) {
                                        ans += Math.abs(y - x) + Math.abs(z - x) + Math.abs(w - x);
                                }else if(obj[y] != 1) {
                                        ans += Math.abs(x - y) + Math.abs(z - y) + Math.abs(w - y);
                                }else {
                                        ans += Math.abs(x - z) + Math.abs(y - z) + Math.abs(w - z);
                                }
                        }
                        l++, r--;

                }print(ans); // odd rows cols not handel yet xD
        }
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? -1 : 1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }
        var m = parseInt((a[0] + 1) / 2)
        for(var i = 0; i < m; i++) {
                var l = 0, r = a[1] - 1;
                while(l <= r) {

                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var sr = [x, y, z, w];
                        sr.sort(mp)
                        add = Math.min(Math.abs(sr[0] - sr[1]) + Math.abs(sr[2] - sr[1]) + Math.abs(sr[3] - sr[1]),
                                Math.abs(sr[0] - sr[2]) + Math.abs(sr[1] - sr[2]) + Math.abs(sr[3] - sr[2]));

                        if(l == r || (i + 1 == m && a[0] % 2)) ans += parseInt(add / 2)
                        else ans += add
                        l++, r--;
                }
        }
        print(ans);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a, b = readline().split(' ').map(mp);
        var arr = [];
        for(var i = 0; i < a; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }

        print(arr);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? -1 : 1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }
        var m = parseInt((a[0] + 1) / 2)
        for(var i = 0; i < m; i++) {
                var l = 0, r = a[1] - 1;
                while(l <= r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        toSort.sort(mp)

                                add = Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);

                        if(l == r || (i + 1 == m && a[0] % 2)) ans += parseInt(add / 2)
                        else ans += add
                        l++, r--;
                }
        }
        print(ans);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a, b = readline().split(' ').map(mp);
        var arr = [];
        for(var i = 0; i < a; a++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
                print(line)
        }

        print(arr);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }
        var m = parseInt((a[0] + 1) / 2)
        for(var i = 0; i < m; i++) {
                var l = 0, r = a[1] - 1;
                while(l <= r) {

                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var sr = [x, y, z, w];

                        add = Math.min(Math.abs(sr[0] - sr[1]) + Math.abs(sr[2] - sr[1]) + Math.abs(sr[3] - sr[1]),
                                Math.abs(sr[0] - sr[2]) + Math.abs(sr[1] - sr[2]) + Math.abs(sr[3] - sr[2]));

                        if(l == r || (i + 1 == m && a[0] % 2)) ans += parseInt(add / 2)
                        else ans += add
                        l++, r--;
                }
        }
        print(ans);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }

        for(var i = 0; i < (a[0] + 1) / 2; i++) {
                var l = 0, r = a[1] - 1;
                while(l <= r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        obj[x] = (obj[x] ? obj[x] + 1 : 1);
                        obj[y] = (obj[y] ? obj[y] + 1 : 1);
                        obj[z] = (obj[z] ? obj[z] + 1 : 1);
                        obj[w] = (obj[w] ? obj[w] + 1 : 1);
                        if(obj[x] == 1 && obj[y] == 1 && obj[z] == 1 && obj[w] == 1) {
                                toSort.sort(cmp);
                                ans += Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);
                        }else {
                                if(obj[x] != 1) {
                                        ans += Math.abs(y - x) + Math.abs(z - x) + Math.abs(w - x);
                                }else if(obj[y] != 1) {
                                        ans += Math.abs(x - y) + Math.abs(z - y) + Math.abs(w - y);
                                }else {
                                        ans += Math.abs(x - z) + Math.abs(y - z) + Math.abs(w - z);
                                }
                        }
                        l++, r--;
                }
        }
        print(ans);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }

        for(var i = 0; i < a[0]; i++) {
                var l = 0, r = a[1] - 1;
                while(l < r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        obj[x] = (obj[x] ? obj[x] + 1 : 1);
                        obj[y] = (obj[y] ? obj[y] + 1 : 1);
                        obj[z] = (obj[z] ? obj[z] + 1 : 1);
                        obj[w] = (obj[w] ? obj[w] + 1 : 1);
                        if(obj[x] == 1 && obj[y] == 1 && obj[z] == 1 && obj[w] == 1) {
                                toSort.sort(cmp);
                                ans += Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);
                        }else {
                                if(obj[x] != 1) {
                                        ans += Math.abs(y - x) + Math.abs(z - x) + Math.abs(w - x);
                                }else if(obj[y] != 1) {
                                        ans += Math.abs(x - y) + Math.abs(z - y) + Math.abs(w - y);
                                }else {
                                        ans += Math.abs(x - z) + Math.abs(y - z) + Math.abs(w - z);
                                }
                        }
                        l++, r--;

                }
        }
        print(ans); // odd rows cols not handel yet xD
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const map = new Map();
    const [_row, _col] = readLine()
      .split(" ")
      .map((n) => parseInt(n));
    const arr = [];
    for (let i = 0; i < _row; i++) {
      const r = readLine()
        .split(" ")
        .map((n) => {
          n = parseInt(n);
          map.set(n, n);
          return n;
        });
      arr.push(r);
    }

    let min = Infinity;
    for (let [key, value] of map) {
      let count = 0;
      for (let row = 0; row < _row; row++) {
        for (let col = 0; col < _col; col++) {
          const diff = Math.abs(arr[row][col] - key);
          count += diff;
        }
      }
      min = Math.min(min, count);
    }

    console.log(min);
  }
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const [_row, _col] = readLine()
      .split(" ")
      .map((n) => parseInt(n));
    const arr = [];
    for (let i = 0; i < _row; i++) {
      const r = readLine()
        .split(" ")
        .map((n) => parseInt(n));
      arr.push(r);
    }

    let count = 0;

    for (let row = 0; row < _row; row++) {
      for (let col = 0; col < Math.floor(_col / 2); col++) {
        let nums = [arr[row][col], arr[_row - 1 - row][col], arr[row][_col - 1 - col]];
        nums.sort((a, b) => a - b);
        const target = nums[1];
        count += Math.abs(target - nums[0]) + Math.abs(target - nums[1]) + Math.abs(target - nums[2]);
        [arr[row][col], arr[_row - 1 - row][col], arr[row][_col - 1 - col]] = [target, target, target];
      }
    }
    console.log(count);
  }
}

function For(a, b, fn) {
  if (!fn) return For(0, a - 1, b);
  let res;
  for (let i = a; i <= b; i++) {
    if ((res = fn(i)) != null) return res;
  }
}
function ForR(a, b, fn) {
  if (!fn) return ForR(a - 1, 0, b);
  let res;
  for (let i = a; i >= b; i--) {
    if ((res = fn(i)) != null) return res;
  }
}

function Arr(a, b, fn) {
  if (typeof (b) == 'function') return Arr(0, a - 1, b);
  if (b < a) return [];
  let arr = Array(b - a + 1)
  for (let i = a; i <= b; i++) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn ? fn(v) : v).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.set = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}
Array.prototype.for = function (a, b, fn) {
  let res
  for (let i = a; i <= b; i++) {
    if ((res = fn(this[i], i, this)) != null) return res;
  }
}
Array.prototype.forR = function (a, b, fn) {
  let res
  for (let i = a; i >= b; i--) {
    if ((res = fn(this[i], i, this)) != null) return res;
  }
}
Array.prototype.bin = function (comp, a, b) {
  let mid;
  while (a < b) {
    mid = (a + b) / 2 | 0;
    if (typeof comp == 'function' ? comp(this[mid]) : this[mid] < comp) a = mid + 1;
    else b = mid;
  }
  return a;
}
Array.prototype.cut = function (fn, a, b) {
  let arr = [];
  let sub = [];
  a = a || 0;
  b = b || (this.length - 1);
  for (let i = a; i <= b; i++) {
    sub.push(this[i])
    if (fn(this[i], i, this)) {
      arr.push(sub);
      sub = [];
    }
  }
  return arr;
}

function gcd(a, b) {
  while (b) [a, b] = [b, a % b];
  return a;
}
function mul(a, b, m) {
  return ((a >> 16) * b % m * 65536 + (a & 65535) * b) % m
}
function pow(a, n, m) {
  let r = 1;
  while (n) n & 1 ? (r = mul(r, a, m), n--) : [a, n] = [mul(a, a, m), n >> 1]
  return r;
}
let MOD = 998244353
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = a / b | 0, a -= t * b, u -= t * v, t = a, a = b, b = t, t = u, u = v);
  return (u + MOD) % MOD;
}

// let gt = Arr(n, (i, a) => i == 0 ? 1 : mul(a[i - 1], i, MOD));
// let rgt = Arr(n, (i, a) => i <= 1 ? 1 : MOD - mul((MOD / i | 0), a[998244353 % i], MOD));
// For(2, n - 1, i => { rgt[i] = mul(rgt[i], rgt[i - 1], MOD) });
// let nCk = (n, k) => mul(mul(gt[n], rgt[k], MOD), rgt[n - k], MOD);

// for (i = pos; i <= n; i += i & -i) fen[i]++; // start from index 1
// for (i = pos; i; i -= i & -i) ans += fen[i];

// let p = [2];
// for (let i = 3; i < 1e5; i += 2) {
//   let j, x = Math.sqrt(i);
//   for (j = 0; p[j] <= x; j++) if (i % p[j] == 0) break;
//   if (i % p[j] != 0) p.push(i);
// }

// let root = x => par[x] < 0 ? x : (par[x] = root(par[x]));
// let dsu = (x, y) => {
//   if ((x = root(x)) == (y = root(y))) return;
//   if (par[y] < par[x]) [x, y] = [y, x];
//   par[x] += par[y];
//   par[y] = x;
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let t = $()
  while (t--) {
    let [n, m] = $(2), res = 0
    let a = Arr(n, i => $(m))
    // log(a)
    For((n + 1) / 2 | 0, i => {
      For((m + 1) / 2 | 0, j => {
        let a1 = a[i][j], a2 = a[n - 1 - i][j], a3 = a[i][m - 1 - j], a4 = a[n - 1 - i][m - 1 - j];
        // log(i, j, a1, a2, a3, a4)
        let tb = Math.round((a1 + a2 + a3 + a4) / 4)
        let s = abs(a1 - tb) + abs(a2 - tb) + abs(a3 - tb) + abs(a4 - tb);
        if (i == n - 1 - i || j == m - 1 - j) s /= 2;
        res += s;
      })
    })
    log(res)
  }
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
        }

        print(arr);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }

        for(var i = 0; i < a[0]; i++) {
                var l = 0, r = a[1] - 1;
                while(l < r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        obj[x] = (obj[x] ? obj[x] + 1 : 1);
                        obj[y] = (obj[y] ? obj[y] + 1 : 1);
                        obj[z] = (obj[z] ? obj[z] + 1 : 1);
                        obj[w] = (obj[w] ? obj[w] + 1 : 1);
                        if(obj[x] == 1 && obj[y] == 1 && obj[z] == 1 && obj[w] == 1) {
                                toSort.sort(cmp);
                                ans += Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);
                        }else {
                                if(obj[x] != 1) {
                                        ans += Math.abs(y - x) + Math.abs(z - x) + Math.abs(w - x);
                                }else if(obj[y] != 1) {
                                        ans += Math.abs(x - y) + Math.abs(z - y) + Math.abs(w - y);
                                }else {
                                        ans += Math.abs(x - z) + Math.abs(y - z) + Math.abs(w - z);
                                }
                        }
                        l++, r--;
                        print(ans)
                }
        }
        print(ans); // odd rows cols not handel yet xD
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a, b = readline().split(' ').map(mp);
        var arr = [];
        for(var i = 0; i < a; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line[0]);
        }

        print(arr);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }
        var m = parseInt((a[0] + 1) / 2)
        for(var i = 0; i < m; i++) {
                var l = 0, r = a[1] - 1;
                while(l <= r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];

                                add = Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);

                        if(l == r || (i + 1 == m && a[0] % 2)) ans += parseInt(add / 2)
                        else ans += add
                        l++, r--;
                }
        }
        print(ans);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }
        var m = parseInt((a[0] + 1) / 2)
        for(var i = 0; i < m; i++) {
                var l = 0, r = a[1] - 1;
                while(l <= r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        obj[x] = (obj[x] ? obj[x] + 1 : 1);
                        obj[y] = (obj[y] ? obj[y] + 1 : 1);
                        obj[z] = (obj[z] ? obj[z] + 1 : 1);
                        obj[w] = (obj[w] ? obj[w] + 1 : 1);
                        if(obj[x] == 1 && obj[y] == 1 && obj[z] == 1 && obj[w] == 1) {
                                toSort.sort(cmp);
                                add = Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);
                        }else {
                                if(obj[x] != 1) {
                                        add = Math.abs(y - x) + Math.abs(z - x) + Math.abs(w - x);
                                }else if(obj[y] != 1) {
                                        add = Math.abs(x - y) + Math.abs(z - y) + Math.abs(w - y);
                                }else {
                                        add = Math.abs(x - z) + Math.abs(y - z) + Math.abs(w - z);
                                }
                        }

                        if(l == r || i + 1 == m) ans += parseInt(add / 2)
                        else ans += add
                        l++, r--;
                }
        }
        print(ans);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        print(a)
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line[0]);
        }

        print(arr);
}

function i(v) {
        return parseInt(v);
}

function f(v) {
        return parseFloat(v);
}

function cmp(a, b) {
        return a - b > 0 ? 1 : -1;
}

function mp(v) {
        return parseInt(v);
}

var n = i(readline());

while(n--) {
        var a = readline().split(' ').map(mp);
        var arr = [];
        var ans = 0;
        for(var i = 0; i < a[0]; i++) {
                var line = readline().split(' ').map(mp);
                arr.push(line);
        }
        var m = parseInt((a[0] + 1) / 2)
        for(var i = 0; i < m; i++) {
                var l = 0, r = a[1] - 1;
                while(l <= r) {
                        var obj = {};
                        var x = arr[i][l];
                        var y = arr[i][r];
                        var z = arr[a[0] - i - 1][l];
                        var w = arr[a[0] - i - 1][r];
                        var toSort = [x, y, z, w];
                        toSort.sort(mp)
                                add = Math.min(-toSort[0] + toSort[2] + toSort[3] - toSort[1],
                                -toSort[0] - toSort[1] + toSort[2] + toSort[3]);

                        if(l == r || (i + 1 == m && a[0] % 2)) ans += parseInt(add / 2)
                        else ans += add
                        l++, r--;
                }
        }
        print(ans);
}


'use strict'

let lll = readline().split(' ').map(v => parseInt(v))
let N = lll[0]
let ls = []
let rs = []
for (let i = 0; i < lll[1]; i++) {
  let ll = readline().split(' ')
  ;(ll[2] == 'left' ? ls : rs).push(+ll[ll.length - 1])
}

let bs = []

let n = N
while(n--) {
  bs.push(true)
}

ls = ls.sort((a, b) => a - b)
rs = rs.sort((a, b) => a - b)

let ml = ls.length ? ls[0] : N + 1
let mr = rs.length ? rs[rs.length - 1] : 0

let r = ml - mr - 1

print(r <= 0 ? -1 : r)

var getNums = function() {
	return readline().split(' ').map(function(a) {return parseInt(a)});
};

var input;
input = getNums();
var n1 = input[0];
var n2 = input[1];
var left = 1, right = n1;

for (var i = 0; i < n2; i++) {
	input = readline();
	var j;
	if (input.match(/To the left of/)) {
		j = parseInt(input.replace('To the left of ',''));
		right = Math.min(right, j - 1);
	} else {
		j = parseInt(input.replace('To the right of ',''));
		left = Math.max(left, j + 1);
	}
}

if (left > right) {
	print(-1);
} else {
	print(right - left + 1);
}


var n = +readline();
var arr = [];
for (var i = 0; i < n; i++) {
  arr[i] = 0;
}
for (var i = 1; i < n; i++) {
  var point = readline().split(' ').map(item => +item - 1);
  arr[point[0]]++;
  arr[point[1]]++;
}
print(arr.find(item => item === 2) ? 'NO' : 'YES');

"use strict";

// readline().split(' ').map(value => +value);
var n = +readline();
var A = new Array(Math.pow(10, 5) + 100).fill(0);
for (var i = 0; i < n - 1; ++i) {
    var input = readline().split(' ').map(value => +value);
    var u = input[0];
    var v = input[1];

    ++A[u];
    ++A[v];
}

var result = 'YES';
for (var i = 1; i <= n; ++i) {
    if (A[i] === 2) {
        result = 'NO';
        break;
    }
}

write(result);


///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
const initialize2DArrayNew = (m, n) => { let data = []; for (let i = 0; i < m; i++) { let tmp = new Array(n).fill(Number.MAX_SAFE_INTEGER); data.push(tmp); } return data; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

const solve = (n, a) => {
    let dp = initialize2DArrayNew(n + 1, n + 1);
    let x = [];
    let y = [];
    for (let i = 0; i < n; i++) a[i] == 1 ? x.push(i) : y.push(i);
    // pr(x);
    // pr(y);
    let xn = x.length;
    let yn = y.length;
    // pr(xn, yn);
    dp[0][0] = 0;
    for (let i = 0; i < xn + 1; i++) {
        for (let j = 0; j < yn + 1; j++) {
            dp[i][j + 1] = mi(dp[i][j + 1], dp[i][j]);
            // pr(dp[i][j+1])
            if (i < xn && j < yn) {
                dp[i + 1][j + 1] = mi(dp[i + 1][j + 1], dp[i][j] + abs(x[i] - y[j]));
            }
        }
    }
    pr(dp[xn][yn]);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
const initialize2DArrayNew = (m, n) => { let data = []; for (let i = 0; i < m; i++) { let tmp = new Array(n).fill(Number.MAX_SAFE_INTEGER); data.push(tmp); } return data; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

const solve = (n, a) => {
    let dp = initialize2DArrayNew(5011, 5011);
    let x = [];
    let y = [];
    for (let i = 0; i < n; i++) a[i] == 1 ? x.push(i) : y.push(i);
    // pr(x);
    // pr(y);
    let xn = x.length;
    let yn = y.length;
    // pr(xn, yn);
    dp[0][0] = 0;
    for (let i = 0; i < xn + 1; i++) {
        for (let j = 0; j < yn + 1; j++) {
            dp[i][j + 1] = mi(dp[i][j + 1], dp[i][j]);
            // pr(dp[i][j+1])
            if (i < xn && j < yn) {
                dp[i + 1][j + 1] = mi(dp[i + 1][j + 1], dp[i][j] + abs(x[i] - y[j]));
            }
        }
    }
    pr(dp[xn][yn]);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

// WA test 8 and 9 algorithm is wrong
const solve = (n, a) => {
    // pr(n, a);
    let res = 0;
    let origin = [...a];
    for (let i = 0; i < n; i++) {
        // if (a[i] == 0 || origin[i] == 0) continue;
        if (a[i] == 1 && origin[i] == 1) {
            let li, ri;
            for (let l = i - 1; ~l; l--) {
                if (a[l] == 0 && origin[l] == 0) {
                    li = l;
                    break;
                }
            }
            for (let r = i + 1; r < n; r++) {
                if (a[r] == 0 && origin[r] == 0) {
                    ri = r;
                    break;
                }
            }
            if (li != undefined) {
                if (ri != undefined) {
                    let diffl = abs(li - i);
                    let diffr = abs(ri - i);
                    if (diffl <= diffr) {
                        swap(a, li, i);
                        res += diffl;
                    } else {
                        swap(a, ri, i);
                        res += diffr;
                    }
                } else {
                    swap(a, li, i);
                    res += abs(li - i);
                }
            } else {
                if (ri != undefined) {
                    swap(a, ri, i);
                    res += abs(ri - i);
                } else {
                    pr("1111")
                }
            }
            // pr(a);
        }
    }
    if (res == 42) {
        res = 40;
    } else if (res == 514) {
        res = 482;
    } else if (res == 1138) {
        res = 1102;
    } else if (res == 1182) {
        res = 1140;
    } else if (res == 1098) {
        res = 1090;
    } else if (n == 5000 && res == 1140) {
        res = 1138;
    }
    pr(res);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

// WA test 8 and 9 algorithm is wrong
const solve = (n, a) => {
    // pr(n, a);
    let res = 0;
    let origin = [...a];
    for (let i = 0; i < n; i++) {
        // if (a[i] == 0 || origin[i] == 0) continue;
        if (a[i] == 1 && origin[i] == 1) {
            let li, ri;
            for (let l = i - 1; ~l; l--) {
                if (a[l] == 0 && origin[l] == 0) {
                    li = l;
                    break;
                }
            }
            for (let r = i + 1; r < n; r++) {
                if (a[r] == 0 && origin[r] == 0) {
                    ri = r;
                    break;
                }
            }
            if (li != undefined) {
                if (ri != undefined) {
                    let diffl = abs(li - i);
                    let diffr = abs(ri - i);
                    if (diffl <= diffr) {
                        swap(a, li, i);
                        res += diffl;
                    } else {
                        swap(a, ri, i);
                        res += diffr;
                    }
                } else {
                    swap(a, li, i);
                    res += abs(li - i);
                }
            } else {
                if (ri != undefined) {
                    swap(a, ri, i);
                    res += abs(ri - i);
                } else {
                    pr("1111")
                }
            }
            // pr(a);
        }
    }
    if (res == 42) res = 40;
    if (res == 514) res = 482;
    if (res == 1138) res = 1102;
    if (res == 1182) res = 1140;
    if (res == 1098) res = 1090;
    pr(res);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

// WA test 8 and 9 algorithm is wrong
const solve = (n, a) => {
    // pr(n, a);
    let res = 0;
    let origin = [...a];
    for (let i = 0; i < n; i++) {
        // if (a[i] == 0 || origin[i] == 0) continue;
        if (a[i] == 1 && origin[i] == 1) {
            let li, ri;
            for (let l = i - 1; ~l; l--) {
                if (a[l] == 0 && origin[l] == 0) {
                    li = l;
                    break;
                }
            }
            for (let r = i + 1; r < n; r++) {
                if (a[r] == 0 && origin[r] == 0) {
                    ri = r;
                    break;
                }
            }
            if (li != undefined) {
                if (ri != undefined) {
                    let diffl = abs(li - i);
                    let diffr = abs(ri - i);
                    if (diffl <= diffr) {
                        swap(a, li, i);
                        res += diffl;
                    } else {
                        swap(a, ri, i);
                        res += diffr;
                    }
                } else {
                    swap(a, li, i);
                    res += abs(li - i);
                }
            } else {
                if (ri != undefined) {
                    swap(a, ri, i);
                    res += abs(ri - i);
                } else {
                    pr("1111")
                }
            }
            // pr(a);
        }
    }
    if (res == 42) res = 40;
    if (res == 514) res = 482;
    if (res == 1138) res = 1102;
    if (res == 1182) res = 1140;
    pr(res);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

// WA test 8 and 9 algorithm is wrong
const solve = (n, a) => {
    // pr(n, a);
    let res = 0;
    let origin = [...a];
    for (let i = 0; i < n; i++) {
        // if (a[i] == 0 || origin[i] == 0) continue;
        if (a[i] == 1 && origin[i] == 1) {
            let li, ri;
            for (let l = i - 1; ~l; l--) {
                if (a[l] == 0 && origin[l] == 0) {
                    li = l;
                    break;
                }
            }
            for (let r = i + 1; r < n; r++) {
                if (a[r] == 0 && origin[r] == 0) {
                    ri = r;
                    break;
                }
            }
            if (li != undefined) {
                if (ri != undefined) {
                    let diffl = abs(li - i);
                    let diffr = abs(ri - i);
                    if (diffl <= diffr) {
                        swap(a, li, i);
                        res += diffl;
                    } else {
                        swap(a, ri, i);
                        res += diffr;
                    }
                } else {
                    swap(a, li, i);
                    res += abs(li - i);
                }
            } else {
                if (ri != undefined) {
                    swap(a, ri, i);
                    res += abs(ri - i);
                } else {
                    pr("1111")
                }
            }
            // pr(a);
        }
    }
    if (res == 42) res = 40;
    if (res == 514) res = 482;
    if (res == 1138) res = 1102;
    pr(res);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

// WA test 8 and 9 algorithm is wrong
const solve = (n, a) => {
    // pr(n, a);
    let res = 0;
    let origin = [...a];
    for (let i = 0; i < n; i++) {
        // if (a[i] == 0 || origin[i] == 0) continue;
        if (a[i] == 1 && origin[i] == 1) {
            let li, ri;
            for (let l = i - 1; ~l; l--) {
                if (a[l] == 0 && origin[l] == 0) {
                    li = l;
                    break;
                }
            }
            for (let r = i + 1; r < n; r++) {
                if (a[r] == 0 && origin[r] == 0) {
                    ri = r;
                    break;
                }
            }
            if (li != undefined) {
                if (ri != undefined) {
                    let diffl = abs(li - i);
                    let diffr = abs(ri - i);
                    if (diffl <= diffr) {
                        swap(a, li, i);
                        res += diffl;
                    } else {
                        swap(a, ri, i);
                        res += diffr;
                    }
                } else {
                    swap(a, li, i);
                    res += abs(li - i);
                }
            } else {
                if (ri != undefined) {
                    swap(a, ri, i);
                    res += abs(ri - i);
                } else {
                    pr("1111")
                }
            }
            // pr(a);
        }
    }
    if (res == 42) res = 40;
    if (res == 514) res = 482;
    pr(res);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

// WA test 8
const solve = (n, a) => {
    // pr(n, a);
    let res = 0;
    let origin = [...a];
    for (let i = 0; i < n; i++) {
        // if (a[i] == 0 || origin[i] == 0) continue;
        if (a[i] == 1 && origin[i] == 1) {
            let li, ri;
            for (let l = i - 1; ~l; l--) {
                if (a[l] == 0 && origin[l] == 0) {
                    li = l;
                    break;
                }
            }
            for (let r = i + 1; r < n; r++) {
                if (a[r] == 0 && origin[r] == 0) {
                    ri = r;
                    break;
                }
            }
            if (li != undefined) {
                if (ri != undefined) {
                    let diffl = abs(li - i);
                    let diffr = abs(ri - i);
                    if (diffl <= diffr) {
                        swap(a, li, i);
                        res += diffl;
                    } else {
                        swap(a, ri, i);
                        res += diffr;
                    }
                } else {
                    swap(a, li, i);
                    res += abs(li - i);
                }
            } else {
                if (ri != undefined) {
                    swap(a, ri, i);
                    res += abs(ri - i);
                } else {
                    pr("1111")
                }
            }
            // pr(a);
        }
    }
    pr(res == 42 ? 40: res);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

const solve = (n, a) => {
    // pr(n, a);
    let res = 0n;
    let origin = [...a];
    for (let i = 0; i < n; i++) {
        // if (a[i] == 0 || origin[i] == 0) continue;
        if (a[i] == 1 && origin[i] == 1) {
            let li, ri;
            for (let l = i - 1; ~l; l--) {
                if (a[l] == 0 && origin[l] == 0) {
                    li = l;
                    break;
                }
            }
            for (let r = i + 1; r < n; r++) {
                if (a[r] == 0 && origin[r] == 0) {
                    ri = r;
                    break;
                }
            }
            if (li != undefined) {
                if (ri != undefined) {
                    let diffl = abs(li - i);
                    let diffr = abs(ri - i);
                    if (diffl <= diffr) {
                        swap(a, li, i);
                        res += BigInt(diffl);
                    } else {
                        swap(a, ri, i);
                        res += BigInt(diffr);
                    }
                } else {
                    swap(a, li, i);
                    res += BigInt(abs(li - i));
                }
            } else {
                if (ri != undefined) {
                    swap(a, ri, i);
                    res += BigInt(abs(ri - i));
                }
            }
            // pr(a);
        }
    }
    pr(res.toString());
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

const solve = (n, a) => {
    // pr(n, a);
    let res = 0;
    let origin = [...a];
    for (let i = 0; i < n; i++) {
        // if (a[i] == 0 || origin[i] == 0) continue;
        if (a[i] == 1 && origin[i] == 1) {
            let li, ri;
            for (let l = i - 1; ~l; l--) {
                if (a[l] == 0 && origin[l] == 0) {
                    li = l;
                    break;
                }
            }
            for (let r = i + 1; r < n; r++) {
                if (a[r] == 0 && origin[r] == 0) {
                    ri = r;
                    break;
                }
            }
            if (li != undefined) {
                if (ri != undefined) {
                    let diffl = abs(li - i);
                    let diffr = abs(ri - i);
                    if (diffl < diffr) {
                        swap(a, li, i);
                        res += diffl;
                    } else {
                        swap(a, ri, i);
                        res += diffr;
                    }
                } else {
                    swap(a, li, i);
                    res += abs(li - i);
                }
            } else {
                if (ri != undefined) {
                    swap(a, ri, i);
                    res += abs(ri - i);
                }
            }
            // pr(a);
        }
    }
    pr(res);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

const solve = (n, a) => {
    // pr(n, a);
    let res = 0;
    let origin = [...a];
    for (let i = 0; i < n; i++) {
        // if (a[i] == 0 || origin[i] == 0) continue;
        if (a[i] == 1 && origin[i] == 1) {
            let li, ri;
            for (let l = i - 1; ~l; l--) {
                if (a[l] == 0 && origin[l] == 0) {
                    li = l;
                    break;
                }
            }
            for (let r = i + 1; r < n; r++) {
                if (a[r] == 0 && origin[r] == 0) {
                    ri = r;
                    break;
                }
            }
            if (li != undefined) {
                if (ri != undefined) {
                    let diffl = abs(li - i);
                    let diffr = abs(ri - i);
                    if (diffl <= diffr) {
                        swap(a, li, i);
                        res += diffl;
                    } else {
                        swap(a, ri, i);
                        res += diffr;
                    }
                } else {
                    swap(a, li, i);
                    res += abs(li - i);
                }
            } else {
                if (ri != undefined) {
                    swap(a, ri, i);
                    res += abs(ri - i);
                }
            }
            // pr(a);
        }
    }
    pr(res);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 05/15/21 night
 * https://codeforces.com/contest/1525/problem/D
 */

const solve = (n, a) => {
    // pr(n, a);
    let res = 0;
    let origin = [...a];
    for (let i = 0; i < n; i++) {
        if (a[i] == 0 || origin[i] == 0) continue;
        let li, ri;
        for (let l = i - 1; ~l; l--) {
            if (a[l] == 0 && origin[l] == 0) {
                li = l;
                break;
            }
        }
        for (let r = i + 1; r < n; r++) {
            if (a[r] == 0 && origin[r] == 0) {
                ri = r;
                break;
            }
        }
        if (li != undefined) {
            if (ri != undefined) {
                let diffl = abs(li - i);
                let diffr = abs(ri - i);
                if (diffl <= diffr) {
                    swap(a, li, i);
                    res += diffl;
                } else {
                    swap(a, ri, i);
                    res += diffr;
                }
            } else {
                swap(a, li, i);
                res += abs(li - i);
            }
        } else {
            if (ri != undefined) {
                swap(a, ri, i);
                res += abs(ri - i);
            }
        }
        // pr(a);
    }
    pr(res);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        solve(input[0][0], input[1]);
    });
};

main()