// C# program to find number of transformation
// to make two Matrix Equal
using System;
class GfG {
static int countOperations(int[, ] a, int[, ] b) {
int n = a.GetLength(0);
int m = a.GetLength(1);
// Create difference matrix (a = a - b)
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i, j] -= b[i, j];
}
}
// Check if transformation is possible using the
// property a[i, j] - a[i, 0] - a[0, j] + a[0, 0]
// should be 0
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if (a[i, j] - a[i, 0] - a[0, j] + a[0, 0]
!= 0) {
return -1;
}
}
}
int result = 0;
// Add operations needed for first column
for (int i = 0; i < n; i++) {
result += Math.Abs(a[i, 0]);
}
// Add operations needed for
// first row (excluding a[0, 0])
for (int j = 0; j < m; j++) {
result += Math.Abs(a[0, j] - a[0, 0]);
}
return result;
}
static void Main(string[] args) {
int[, ] a = { { 1, 1 }, { 1, 1 } };
int[, ] b = { { 1, 2 }, { 3, 4 } };
Console.WriteLine(countOperations(a, b));
}
}