Example

Input

3 3 1 2 4
1 2
2 3
3 1


Output

IMPOSSIBLE
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef pair<int,int> P;

int main(){
    int N,M;
    vector<int> A(3);
    cin >> N >> M;
    for(int i=0;i<3;i++) cin >> A[i];
    vector<vector<int>> g(N);
    vector<vector<P>> g2(N);
    for(int i=0;i<M;i++){
        int a,b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
    }
    
    auto bfs = [&](int s){
        vector<vector<int>> vis(N,vector<int>(101,0));
        queue<P> Q;
        Q.push(P(s,0));
        vis[s][0] = 1;
        while(!Q.empty()){
            P now = Q.front(); Q.pop();
            for(auto& e:g[now.first]){
                if(!vis[e][now.second+1]){
                    vis[e][now.second+1] = 1;
                    if(now.second+1<100) Q.push({e,now.second+1});
                }
            }
        }
        for(int i=0;i<N;i++) for(int j=0;j<3;j++) if(vis[i][A[j]]){
            g2[s].push_back(P(i,j));
        }
    };

    for(int i=0;i<N;i++) bfs(i);
    int inf = 1e9;
    vector<int> dp(N,inf);
    dp[N-1] = 0;
    while(true){
        bool update = false;
        for(int i=0;i<N;i++){
            vector<int> mi(3,inf);
            for(auto& e:g2[i]){
                mi[e.second] = min(mi[e.second],dp[e.first]);
            }
            int res = 0;
            for(int j=0;j<3;j++) res = max(res,mi[j]+1);
            if(dp[i]>res){
                update = true;
                dp[i] = res;
            }
        }
        if(!update) break;
    }
    if(dp[0]==inf) cout << "IMPOSSIBLE" << endl;
    else cout << dp[0] << endl;
}
