#include <bits/stdc++.h>
using namespace std;

typedef vector<vector<int> > mat;

const int R = 4;
const int C = 4;
const int di[4] = {0,1,0,-1};
const int dj[4] = {1,0,-1,0};

int ans;

struct Water {
  int i, j, k;
};

bool finished(const mat &g) {
  for(int i = 0; i < R; ++i) {
    for(int j = 0; j < C; ++j) {
      if(g[i][j]) return false;
    }
  }
  return true;
}

mat click(int pi, int pj, mat g) {
  g[pi][pj] += 1;
  if(g[pi][pj] < 5) return g;
  g[pi][pj] = 0;
  vector<Water> ws;
  for(int k = 0; k < 4; ++k) {
    ws.push_back((Water){pi, pj, k});
  }
  while(ws.size()) {
    vector<Water> nws;
    map<pair<int, int>, int> m;
    for(int i = 0; i < ws.size(); ++i) {
      Water &w = ws[i];
      w.i += di[w.k];
      w.j += dj[w.k];
      if(w.i < 0 || w.i >= R) continue;
      if(w.j < 0 || w.j >= C) continue;
      if(g[w.i][w.j]) {
        m[make_pair(w.i, w.j)] += 1;
        continue;
      }
      nws.push_back(w);
    }
    for(map<pair<int, int>, int>::iterator it = m.begin();
        it != m.end(); ++it) {
      int i = it->first.first;
      int j = it->first.second;
      int c = it->second;
      if((g[i][j] += c) < 5) continue;
      g[i][j] = 0;
      for(int k = 0; k < 4; ++k) {
        nws.push_back((Water){i, j, k});
      }
    }
    ws = nws;
  }
  return g;
}

void rec(mat g, int c) {
  if(c >= ans) return;
  if(finished(g)) {
    ans = c;
    return;
  }
  for(int i = 0; i < R; ++i) {
    for(int j = 0; j < C; ++j) {
      if(c+1 >= ans) return;
      rec(click(i, j, g), c + 1);
    }
  }
}

int main() {
  while(1) {
    mat g(R, vector<int>(C));
    for(int i = 0; i < R; ++i) {
      for(int j = 0; j < C; ++j) {
        if(cin >> g[i][j]); else return 0;
      }
    }
    ans = 6;
    rec(g, 0);
    if(ans == 6) ans = -1;
    cout << ans << endl;
  }
  return 0;
}