#include <bits/stdc++.h>

using namespace std;

const int vy[] = {-1, 0, 1, 0};
const int vx[] = {0, 1, 0, -1};


void bfs(vector< int > &s, int idx)
{

  queue< pair< int, int > > que;
  queue< int > beet;
  que.emplace(idx, -1);
  vector< int > bomb(16, 114514);
  beet.emplace(0);

  while(!que.empty()) {
    auto p = que.front();
    que.pop();

    int latte = beet.front();
    beet.pop();
    int xx = p.first % 4, yy = p.first / 4;

    if(s[p.first] <= 4) {
      ++s[p.first];
      if(s[p.first] == 5) {
        bomb[p.first] = latte;
        for(int i = 0; i < 4; i++) {
          int nx = xx + vx[i], ny = yy + vy[i];
          if(nx < 0 || ny < 0 || nx >= 4 || ny >= 4) continue;
          que.emplace(ny * 4 + nx, i);
          beet.emplace(latte + 1);
        }
      }
    } else if(latte != bomb[p.first]) {
      int nx = xx + vx[p.second], ny = yy + vy[p.second];
      if(nx < 0 || ny < 0 || nx >= 4 || ny >= 4) continue;
      que.emplace(ny * 4 + nx, p.second);
      beet.emplace(latte + 1);
    }
  }

}

int main()
{
  int N;
  vector< int > vec(16);

  for(int i = 0; i < 16; i++) {
    cin >> vec[i];
    if(vec[i] == 0) vec[i] = 5;
  }

  map< vector< int >, int > dp;
  queue< vector< int > > que;
  dp[vec] = 0;
  que.emplace(vec);

  while(!que.empty()) {
    auto p = que.front();
    int cost = dp[p];
    que.pop();
    if(count(begin(p), end(p), 5) == 16) {
      cout << cost << endl;
      return (0);
    }

    if(cost == 5) {
      continue;
    }

    for(int i = 0; i < 16; i++) {
      if(p[i] == 5) continue;
      auto q = p;
      bfs(q, i);
      if(dp.count(q)) continue;
      dp[q] = cost + 1;
      que.emplace(q);
    }
  }
  cout << -1 << endl;
}