1 #include "bits/stdc++.h"
2 using namespace std;
3 typedef long long LL;
4 const int MAX=105;
5 int n,m;
6 int tot,head[MAX],adj[MAX*MAX],next[MAX*MAX],wei[MAX*MAX][2];
7 int f[MAX][10005],g[MAX][10005];
8 inline int read(){
9 int an=0,x=1;char c=getchar();
10 while (c<‘0‘ || c>‘9‘) {if (c==‘-‘) x=-1;c=getchar();}
11 while (c>=‘0‘ && c<=‘9‘) {an=an*10+c-‘0‘;c=getchar();}
12 return an*x;
13 }
14 void addedge(int u,int v,int w1,int w2){
15 tot++;
16 adj[tot]=v;
17 wei[tot][1]=w1,wei[tot][2]=w2;
18 next[tot]=head[u];
19 head[u]=tot;
20 }
21 int main(){
22 freopen ("time.in","r",stdin);freopen ("time.out","w",stdout);
23 int i,j,k;
24 int u,v,w1,w2;
25 n=read(),m=read();
26 for (i=1;i<=m;i++){
27 u=read(),v=read(),w1=read(),w2=read();
28 addedge(u,v,w1,w2);
29 }
30 memset(f,false,sizeof(f)),memset(g,false,sizeof(g));
31 f[1][0]=g[1][0]=true;
32 for (i=1;i<n;i++){
33 for (j=head[i];j;j=next[j]){
34 for (k=wei[j][1];k<=10000;k++) f[adj[j]][k]|=f[i][k-wei[j][1]];
35 for (k=wei[j][2];k<=10000;k++) g[adj[j]][k]|=g[i][k-wei[j][2]];
36 }
37 }
38 for (i=1;i<=10000;i++)
39 if (f[n][i] && g[n][i])
40 return printf("%d",i),0;
41 puts("IMPOSSIBLE");
42 return 0;
43 }