#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <utility>
#include <tuple>
#include <iostream>
using namespace std;

const int N = 4;
using State = vector< vector<int> >;
map<State, int> memo;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

pair<State, bool> get_next_state(State state, int a, int b) {
    (state[a][b] += 1) %= 5;
    
    queue< tuple<int, int, int> > que;
    // 割れたやつ
    if(state[a][b] == 0) {
        for(int k=0; k<4; k++) {
            que.emplace(a, b, k);
        }
    }

    while(que.size()) {
        queue< tuple<int, int, int> > swp;

        // 1 秒動かす
        while(que.size()) {
            auto cur = que.front(); que.pop();
            int x, y, dir; tie(x, y, dir) = cur;
            int nx = x + dx[dir], ny = y + dy[dir];
            if(nx < 0 or ny < 0 or nx >= N or ny >= N) continue;

            if(state[nx][ny] > 0) {
                state[nx][ny]++;
            }
            else {
                swp.emplace(nx, ny, dir);
            }
        }

        // 割れたやつ
        for(int i=0; i<N; i++) {
            for(int j=0; j<N; j++) {
                if(state[i][j] >= 5) {
                    state[i][j] = 0;
                    for(int k=0; k<4; k++) {
                        swp.emplace(i, j, k);
                    }
                }
            }
        }

        swap(que, swp);
    }

    int rem = 0;
    for(int i=0; i<N; i++) {
        for(int j=0; j<N; j++) {
            rem |= state[i][j];
        }
    }
    return make_pair(state, !rem);
}

int main() {
    vector< vector<int> > board(N, vector<int>(N, 0));

    int empty_board = true;
    for(int i=0; i<N; i++) {
        for(int j=0; j<N; j++) {
            cin >> board[i][j];
            empty_board &= (board[i][j] == 0);
        }
    }

    if(empty_board) {
        cout << 0 << endl;
        return 0;
    }

    queue<State> que;
    memo[board] = 0;
    que.push(board);

    while(que.size()) {
        State state = que.front(); que.pop();
        int cost = memo[state];
        if(cost >= 5) continue;
        
        for(int i=0; i<N; i++) {
            for(int j=0; j<N; j++) {
                // if(i != 1 or j != 1) continue;

                /*
                fprintf(stderr, "current state:");
                for(int x=0; x<N; x++) {
                    for(int y=0; y<N; y++) {
                        fprintf(stderr, "%d ", state[x][y]);
                    }
                    fprintf(stderr, "\n");
                }
                */
                
                auto info = get_next_state(state, i, j);

                /*
                fprintf(stderr, "next state:");
                for(int x=0; x<N; x++) {
                    for(int y=0; y<N; y++) {
                        fprintf(stderr, "%d ", info.first[x][y]);
                    }
                    fprintf(stderr, "\n");
                }
                */
                
                if(info.second == true) {
                    cout << cost + 1 << endl;
                    return 0;
                }
                else {
                    if(memo.count(info.first)) continue;
                    memo[info.first] = cost + 1;
                    que.push(info.first);
                }
            }
        }
    }
    cout << -1 << endl;
    return 0;
}



Measured memory_bytes: 28056