using System;
using System.Collections.Generic;
class Program
{
static int MinChanges(List<List<int>> a)
{
int res = 0;
int N = a.Count, M = a[0].Count;
Dictionary<int, Dictionary<int, int>> mp = new Dictionary<int,
Dictionary<int, int>>();
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
int ind = i + j;
if (!mp.ContainsKey(ind))
mp[ind] = new Dictionary<int, int>();
if (!mp[ind].ContainsKey(a[i][j]))
mp[ind][a[i][j]] = 0;
mp[ind][a[i][j]]++;
}
}
int r = M + N - 2, l = 0;
while (l < r)
{
int s = 0, mx = 0;
if (!mp.ContainsKey(r))
mp[r] = new Dictionary<int, int>();
foreach (var x in mp[r])
{
if (!mp.ContainsKey(l))
mp[l] = new Dictionary<int, int>();
if (!mp[l].ContainsKey(x.Key))
mp[l][x.Key] = 0;
mp[l][x.Key] += x.Value;
}
foreach (var x in mp[l])
{
s += x.Value;
mx = Math.Max(x.Value, mx);
}
res += (s - mx);
l++;
r--;
}
return res;
}
static void Main(string[] args)
{
List<List<int>> mat = new List<List<int>>()
{
new List<int>() { 1, 4, 1 },
new List<int>() { 2, 5, 3 },
new List<int>() { 1, 3, 1 }
};
Console.WriteLine("Total number of changes required: " + MinChanges(mat));
List<List<int>> mat1 = new List<List<int>>()
{
new List<int>() { 1, 4 },
new List<int>() { 2, 5 },
new List<int>() { 1, 3 },
new List<int>() { 2, 5 }
};
Console.WriteLine("Total number of changes required: " + MinChanges(mat1));
}
}