#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <complex>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cassert>
#include <cstring>
using namespace std;
#define REP(i, x, n) for(int i = (int)(x); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(n) n.begin(),n.end()
typedef pair<int, int> P;
typedef pair<P,int> Pi;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
static const int INF = (1 << 29);
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
const int dx[] = {0,1,0,-1};
const int dy[] = {1,0,-1,0};
int blwcnt;
/*
void dfs(VVI &G,int pos)
{
 
 
}
*/
bool check(VVI &G)
{
  rep(i,4)
    rep(j,4)
    if(!G[i][j])
      continue;
    else if(G[i][j] != -1)
    return false;
  return true;
}
 
 
P getIndex(int prev){ return P(prev%4,prev/4); }
 
/*
void click(VVI &G,P p)
{
  G[p.s][p.f]++;
  if(G[p.s][p.f] >= 5)
  {
    blwcnt++;
    G[p.s][p.f] = -1;
    rep(i,4)
    {
      int nx = p.f + dx[i];
      int ny = p.s + dy[i];
       
      while(0 <= nx && nx < 4 && 0 <= ny && ny < 4 && G[ny][nx] == -1)
        nx += dx[i],ny += dy[i];
      if(!(0 <= nx && nx < 4 && 0 <= ny && ny < 4))
        continue;
      
      click(G,P(nx,ny));
    }
  }
}
*/
 
void bomb(VVI &G)
{
  rep(i,4)
    rep(j,4)
    if(G[i][j] >= 5)
      G[i][j] = -1,blwcnt++;
}
 
void print(VVI G)
{
  rep(i,4)
  {
    rep(j,4)
    {
      if(G[i][j] == -1)
        cout << 0 << " ";
      else
        cout << G[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;
}
 
void click(VVI &G,P pos)
{
  //print(G);
   
  G[pos.s][pos.f]++;
  //print(G);
   
  if(G[pos.s][pos.f] < 5)
    return;
  deque<Pi> deq;
  int sec = 0;
  deq.push_back(Pi(pos,0));
   
  while(!deq.empty())
  {
    Pi p = deq.front(); deq.pop_front();     
    //cout << "here" << endl;
      rep(i,4)
      {
        int nx = p.f.f + dx[i];
        int ny = p.f.s + dy[i];
         
        while(0 <= nx && nx < 4 && 0 <= ny && ny < 4 && ( G[ny][nx] == -1 || G[ny][nx] == 0))
          nx += dx[i],ny += dy[i];
        if(!(0 <= nx && nx < 4 && 0 <= ny && ny < 4))
          continue;
        assert(G[ny][nx] != -1 && G[ny][nx]);
        G[ny][nx]++;
        if(G[ny][nx] == 5)
        {
          P np = P(nx,ny);
          //cout << "push! " << np.f << " " << np.s << endl;
          deq.push_back(Pi(np,p.s+1));
        }
       
      }
       
      if(!deq.empty() && deq.front().s != sec)
      {
        bomb(G);
        sec++;
        //print(G);
      }
      
  }
  bomb(G);
}
 
 
 
int main() {
 
  VVI G(4,VI(4));
  int dif = 0;
  rep(i,4)
    rep(j,4)
  {
    cin >> G[i][j];
    if(!G[i][j])dif++;
  }
 
  if(dif == 16)
  {
    cout << 0 << endl;
    return 0;
  }
 
  //click(G,P(2,1));
  //print(G);
 
  //return 0;
  int ans = (1<<29);
  REP(i,0,16)
  {
    REP(j,i,16)
    {
      REP(k,j,16)
      {
        REP(l,k,16)
        {
          REP(m,l,16)
          {
            vector<P> pos(5);
            pos[0] = getIndex(i);
            pos[1] = getIndex(j);
            pos[2] = getIndex(k);
            pos[3] = getIndex(l);
            pos[4] = getIndex(m);
            // do
            //{
              VVI cG = G;
              int cnt = 0;
              blwcnt = 0;
              rep(n,5)
              {
                P p = pos[n];
                if(cG[p.s][p.f] == -1)
                  break;
                click(cG,p);
                cnt++;
                if(cnt >= ans)
                  break;
 
              }
              if(cnt >= ans)
                continue;
              //cout << "blwcnt = " << blwcnt << endl;
              //assert(blwcnt+dif <= 16);
              //assert(cnt);
              //if(blwcnt+dif == 16)
              //ans = min(ans,cnt);
              if(check(cG))
                ans = min(ans,cnt);
              if(ans == 1)
              {
                //rep(Y,5)
                //cout << pos[Y].f << "," << pos[Y].s << endl;
                goto F;
              }
 
              //}while(next_permutation(all(pos)));
 
          }
        }
      }
    }
  }
  F:;
  ans == (1<<29)?cout << -1 << endl:cout << ans << endl;
 
  return 0;
}

Measured memory_bytes: 1224