//Δ_1003 #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef double DB; const int N = 111111; const int A = 1111; const int B = 11; const int inf = 1e9; void cmin(int&x,int y){ if(x>y) x=y; } int n,m,a[N],b[N],f[A*2][B]; int main() { int i,o,k,p; LL s; while(cin>>n>>m){ for(o=0;o>a[i]>>b[i]; while(m--){ cin>>k>>p; for(o=0;o=1;i=i-1) cmin(f[i-1][o],f[i][o]); s=0; p=0; for(i=1;i<=n;i=i+1){ k=f[a[i]][b[i]]; if(k>=inf) p=1; s+=k; } if(p) s=-1; cout<