#include <iostream>
#include <cstring>
using namespace std;
#define MAXN 18
#define INF 987654321
int map[MAXN][MAXN];
int copy_map[MAXN][MAXN];
bool used[MAXN];
int N;
int ans = INF;
void check_map(int x, int y, int arr[][MAXN]){
if (y>0) arr[x][y-1] = (arr[x][y-1]+1) %2;
if (x < N-1){
arr[x+1][y] = (arr[x+1][y]+1)%2;
}
if (x > 0){
arr[x-1][y] = (arr[x-1][y]+1)%2;
}
if (y < N-1) arr[x][y+1] = (arr[x][y+1]+1)%2;
arr[x][y] = (arr[x][y]+1)%2;
}
void dfs(int idx, int cnt){
memcpy(copy_map,map,sizeof(map));
int sum = 0;
for (int i =0; i < N; i++){
if (used[i]){
sum++;
check_map(0,i,copy_map);
}
}
for (int i = 1; i <N; i++){
for (int j =0 ;j<N;j ++){
if (copy_map[i-1][j]){
sum++;
check_map(i,j,copy_map);
}
}
}
bool flag = false;
for (int i = 0; i <N&&!flag; i++){
if (copy_map[N-1][i]) flag = true;
}
if (!flag){
if (ans > sum) ans = sum;
}
if (cnt == N) return;
for (int i = idx; i < N; i++){
if (!used[i]){
used[i] = true;
dfs(i,cnt+1);
used[i] = false;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
for (int i =0 ;i < N; i++){
for (int j =0;j<N;j++){
cin >> map[i][j];
}
}
dfs(0,0);
if (ans == INF) cout <<"-1\n";
else cout << ans <<"\n";
return 0;
}