A binary matrix is called good if every even length square sub-matrix has an odd number of ones. 

Given a binary matrix a consisting of n rows and m columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all. 

All the terms above have their usual meanings — refer to the Notes section for their formal definitions. 

Input

The first line of input contains two integers n and m (1 ≤ n ≤ m ≤ 10^6 and n⋅ m ≤ 10^6) — the number of rows and columns in a, respectively. 

The following n lines each contain m characters, each of which is one of 0 and 1. If the j-th character on the i-th line is 1, then a_{i,j} = 1. Similarly, if the j-th character on the i-th line is 0, then a_{i,j} = 0.

Output

Output the minimum number of cells you need to change to make a good, or output -1 if it's not possible at all.

Examples

Input


3 3
101
001
110


Output


2

Input


7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011


Output


-1

Note

In the first case, changing a_{1,1} to 0 and a_{2,2} to 1 is enough. 

You can verify that there is no way to make the matrix in the second case good. 

Some definitions — 

  * A binary matrix is one in which every element is either 1 or 0. 
  * A sub-matrix is described by 4 parameters — r_1, r_2, c_1, and c_2; here, 1 ≤ r_1 ≤ r_2 ≤ n and 1 ≤ c_1 ≤ c_2 ≤ m. 
  * This sub-matrix contains all elements a_{i,j} that satisfy both r_1 ≤ i ≤ r_2 and c_1 ≤ j ≤ c_2. 
  * A sub-matrix is, further, called an even length square if r_2-r_1 = c_2-c_1 and r_2-r_1+1 is divisible by 2. 
import java.io.*;
import java.util.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
static class cell{
	int r, c;
	public cell(int r, int c) {
		this.r = r;
		this.c = c;
	}
}
static class Pair<E, V> implements Comparable<Pair<E, V>>{
       E a;
       V b;
       public Pair(E x, V y) {a = x;b=y;}
       public int compareTo(Pair<E, V> p){
           return Integer.compare((Integer)a, (Integer)p.a);
       }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + (Integer)a;
            result = prime * result + (Integer)b;
           
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            Pair<E, V> cur = (Pair<E, V>)obj;
            if((Integer)a == (Integer)cur.a && (Integer)b == (Integer)cur.b)return true;
            if((Integer)b == (Integer)cur.a && (Integer)a == (Integer)cur.b)return true;
            return false;
        }
 } 
public static void main(String[] args) throws Exception {
   new Thread(null, null, "Anshum Gupta", 99999999) {
        public void run() {
            try {
                solve();
            } catch(Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }.start();
}
public static long gcd(long a,long b)
{
  if(a<b)
    return gcd(b,a);
  if(b==0)
    return a;
  return gcd(b,a%b);
  
}
static long lcm(int a,int b) {
  return  a*b / gcd(a,b);
}
static int[] z(char[]arr) {
    int l = 0, r = 0;
    int[]z = new int[n];
    for(int i=1; i<n; i++) {
        if(i > r) {
            l = r = i;
            while(r < n && arr[r-l] == arr[r])r++;
            z[i] = r-- - l;
        }else {
            int k = i - l;
            if(z[k] < r - i + l)z[i] = z[k];
            else {
                l = i;
                while(r < n && arr[r-l] == arr[r])r++;
                z[i] = r-- - l;
            }
        }
    }
    return z;
}   
static long pow(long x,long y){
    if(y == 0)return 1;
    if(y==1)return x;
    long a = pow(x,y/2);
    a = (a*a)%mod;
    if(y%2==0){
        return a;
    }
    return (a*x)%mod;
}
static long my_inv(long a) {
    return pow(a,mod-2);
}
static long bin(int a,int b) {
    if(a < b || a<0 || b<0)return 0;
    return ((fact[a]*inv_fact[a-b])%mod * inv_fact[b])%mod;
}
static void make_facts() {
  fact=new long[mxN];
  inv_fact = new long[mxN];
  fact[0]=inv_fact[0]=1L;
  for(int i=1;i<mxN;i++) {
       fact[i] = (i*fact[i-1])%mod;
//       inv_fact[i] = my_inv(fact[i]);
  }
}
static final long mxx = (long)(1e18+5);
static final int mxN = (int)(1e6+5);
static final int mxV = (int)(1e5+5), log = 18;
static long mod = (long)(1e9+7); //998244353;//
static long[]fact, inv_fact;
static final int INF = (int)1e9;
static boolean[]vis;
static ArrayList<ArrayList<Integer>> adj;
static int n, m, k, x, y, z, q;
static char[]arr, str;
static char[][]a;
static char[][] transpose(char[][]a){
	int x = a.length;
	int y = a[0].length;
	char[][]ans = new char[y][x];
	for(int i=0; i<x; i++) {
		for(int j=0; j<y; j++) {
			ans[j][i] = a[i][j];
		}
	}
	return ans;
}
static boolean works(boolean[][]board) {
	// 2 * 2
	for(int i=0; i+1 < board.length; i++) {
		for(int j=0; j+1 < board[i].length; j++) {
			int count = 0;
			for(int x=0; x<2; x++) {
				for(int y=0; y<2; y++) {
					if(board[x+i][y+j])
						count++;
				}
			}
			if(count % 2 == 0)return false;
		}
	}
	return true;
}
	public static void solve() throws Exception {
	   // solve the problem here
			s = new MyScanner();
	   		out = new PrintWriter(new BufferedOutputStream(System.out), true);
//	   		out = new PrintWriter("output.txt");
	        int tc = 1;//s.nextInt();
	        while(tc-->0){
	        	int[] bitsIn=new int[1<<7];
	    		for (int i=0; i<1<<7;i++) bitsIn[i]=Integer.bitCount(i);
	        	n = s.nextInt();
	        	m = s.nextInt();
	        	a = s.next2DCharArray(n, m);
	        	if(Math.min(n, m) > 3) {
	        		out.println("-1");
	        		continue;
	        	}
	        	if(Math.min(m, n) == 1) {
	        		out.println("0");
	        		continue;
	        	}
	        	if(m > n) {
	        		a = transpose(a);
	        		int temp = m;
	        		m = n; 
	        		n = temp;
	        	}
	        	boolean[][]possibleTrans = new boolean[1<<m][1<<m];
	        	for(int m1 = 0; m1 < 1<<m; m1++) {
	        		for(int m2 = 0; m2 < 1<<m; m2++) {
	        			boolean[][]mat = new boolean[m][2];
	        			for(int k=0; k<m; k++) {
	        				mat[k][0] = (m1 & (1<<k)) != 0;
	        			}
	        			for(int k=0; k<m; k++) {
	        				mat[k][1] = (m2 & (1<<k)) != 0;
	        			}
	        			possibleTrans[m1][m2] = works(mat);
	        		}
	        	}
	        	int[]dp = new int[1<<m];
	        	int[]masks = new int[n];
	        	for(int i=0; i<n; i++) {
	        		int mask = 0;
	        		for(int j=0; j<m; j++) {
	        			if(a[i][j] == '1')
	        				mask += (1<<j);
	        		}
	        		masks[i] = mask;
	        	}
	        	dp[masks[0]] = 0;
	        	for(int i=0; i < (1<<m); i++) {
	        		dp[i] = bitsIn[i ^ masks[0]];
	        	}
	        	for(int i=1; i<n; i++) {
	        		int[]costs = new int[1<<m];
	        		Arrays.fill(costs, INF);
	        		for(int j=0; j < 1<<m; j++) {
	        			for(int k=0; k< 1<<m; k++) {
	        				if(possibleTrans[k][j]) {
	        					costs[j] = Math.min(costs[j], dp[k] + bitsIn[j ^ masks[i]]);
	        				}
	        			}
	        		}
	        		dp = costs;
	        	}
	        	int ans = dp[0];
	        	for(int i=0; i<(1<<m); i++) {
	        		ans = Math.min(ans, dp[i]);
	        	}
	        	out.println(ans < INF ? ans : "-1");
	        } 
	           
	        out.flush();
	        out.close();
	}

public static PrintWriter out;
public static MyScanner s;
static class MyScanner {

    BufferedReader br;
    StringTokenizer st;

    public MyScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    public MyScanner(String fileName) {
    	try {
			br = new BufferedReader(new FileReader(fileName));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
    }
    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() { return Integer.parseInt(next()); }
    long nextLong() { return Long.parseLong(next()); }
    double nextDouble() { return Double.parseDouble(next()); }
    int[] nextIntArray(int n){
    	int[]a = new int[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextInt();
    	}
    	return a;
    }
    long[] nextLongArray(int n) {
    	long[]a = new long[n];
    	for(int i=0; i<n; i++) {
    		a[i] = this.nextLong();
    	}
    	return a;
    }
    char[][] next2DCharArray(int n, int m){
    	char[][]arr = new char[n][m];
    	for(int i=0; i<n; i++) {
    		arr[i] = this.next().toCharArray();
    	}
    	return arr;
    }
    ArrayList<ArrayList<Integer>> readUndirectedUnweightedGraph(int n, int m) {
    	ArrayList<ArrayList<Integer>>adj = new ArrayList<ArrayList<Integer>>();
    	for(int i=0; i<=n; i++)adj.add(new ArrayList<Integer>());
    	for(int i=0; i<m; i++) {
    		int u = s.nextInt();
    		int v = s.nextInt();
    		adj.get(u).add(v);
    		adj.get(v).add(u);
    	}
    	return adj;
    }
    ArrayList<ArrayList<Integer>> readDirectedUnweightedGraph(int n, int m) {
    	ArrayList<ArrayList<Integer>>adj = new ArrayList<ArrayList<Integer>>();
    	for(int i=0; i<=n; i++)adj.add(new ArrayList<Integer>());
    	for(int i=0; i<m; i++) {
    		int u = s.nextInt();
    		int v = s.nextInt();
    		adj.get(u).add(v);
    	}
    	return adj;
    }
    String nextLine(){
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}

 
}
