Example

Input

3 3 1 2 4
1 2
2 3
3 1


Output

IMPOSSIBLE
#include <iostream>
#include <vector>
#include <queue>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
using Graph = std::vector<std::vector<Edge<Cost>>>;

using lint = long long;

constexpr int S = 100;

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<int> ss(3);
    for (auto& s : ss) std::cin >> s;

    Graph<> rgraph(n);
    while (m--) {
        int u, v;
        std::cin >> u >> v;
        --u, --v;
        rgraph[v].emplace_back(v, u);
    }

    std::vector<std::vector<lint>> rss(n, std::vector<lint>(S + 1, 0));
    for (int v = 0; v < n; ++v) {
        rss[v][0] = (1LL << v);
    }
    for (int x = 1; x <= S; ++x) {
        for (int v = 0; v < n; ++v) {
            for (auto e : rgraph[v]) {
                int u = e.dst;
                rss[v][x] |= rss[u][x - 1];
            }
        }
    }

    std::vector<int> dist(n, -1), flag(n, 0);
    dist[n - 1] = 0, flag[n - 1] = 0b111;
    for (int d = 0;; ++d) {
        std::queue<int> que;
        for (int v = 0; v < n; ++v) {
            if (dist[v] == d) que.push(v);
        }
        if (que.empty()) break;

        while (!que.empty()) {
            int v = que.front();
            que.pop();

            for (int i = 0; i < 3; ++i) {
                for (int u = 0; u < n; ++u) {
                    if (!((rss[v][ss[i]] >> u) & 1) ||
                        (flag[u] >> i) & 1) continue;

                    flag[u] |= (1 << i);
                    if (flag[u] == 0b111) dist[u] = d + 1;
                }
            }
        }
    }

    if (dist[0] == -1) {
        std::cout << "IMPOSSIBLE" << std::endl;
    } else {
        std::cout << dist[0] << std::endl;
    }
}

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}

