@$_=glob<>for P..V;@a=[1,@Q];$t++while@a=map{($i,$c,$w)=@$_;$w-$t?$_:$i-"@P"?map$S[$_]-$i||$c<$U[$_]?():[$T[$_],$c-$U[$_],$V[$_]+$t],0..@S:exit!print$t}@a;print-1