Example

Input

3 3 1 2 4
1 2
2 3
3 1


Output

IMPOSSIBLE
#include <iostream>
#include <vector>
#include <set>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
using namespace std;
int main() {
    int n, m, a, b, c; cin >> n >> m >> a >> b >> c;
    vector<vector<int> > g(n);
    repeat (i,m) {
        int u, v; cin >> u >> v;
        -- u; -- v;
        g[u].push_back(v);
    }
    vector<vector<bool> > ag, bg, cg; {
        vector<vector<bool> > x(n, vector<bool>(n)); // adjacency matrix
        repeat (i,n) {
            for (int j : g[i]) {
                x[i][j] = true;
            }
        }
        vector<vector<bool> > y = x;
        repeat (t, max(a,max(b,c))) {
            if (a == t+1) ag = y;
            if (b == t+1) bg = y;
            if (c == t+1) cg = y;
            vector<vector<bool> > z(n, vector<bool>(n));
            repeat (i,n) {
                repeat (j,n) {
                    repeat (k,n) {
                        z[i][j] = z[i][j] or (y[i][k] and x[k][j]);
                    }
                }
            }
            y = z;
        }
    }
    set<int> goals;
    goals.insert(n-1);
    for (int turn = 1; ; ++ turn) {
        vector<int> new_goals;
        repeat (i,n) if (not goals.count(i)) {
            bool ok_a = false; repeat (j,n) if (ag[i][j] and goals.count(j)) ok_a = true;
            bool ok_b = false; repeat (j,n) if (bg[i][j] and goals.count(j)) ok_b = true;
            bool ok_c = false; repeat (j,n) if (cg[i][j] and goals.count(j)) ok_c = true;
            if (ok_a and ok_b and ok_c) {
                new_goals.push_back(i);
            }
        }
        if (new_goals.empty()) {
            cout << "IMPOSSIBLE" << endl;
            break;
        }
        goals.insert(new_goals.begin(), new_goals.end());
        if (goals.count(0)) {
            cout << turn << endl;
            break;
        }
    }
    return 0;
}