/*...........................
.........tank.jpg............
...........................*/

//PointBlank's code (⌐■_■)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template<typename T> using pxx = pair<T, T>;
 
ll power(ll x, ll y); 
#define repp(I, A, B) for(int I = A; I <= B; I++)
#define rep(I, A, B) for(int I = A; I < B; I++)
#define rrep(I, B, A) for(int I = B; I >= A; I--)
#define pb emplace_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define LB lower_bound
#define UB upper_bound
#define MP make_pair
#define quickio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(this) cerr<<"> "<<#this<<" : "<<this<<"\n"
#define bitcount(n) __builtin_popcountll(n)
#define in_edges(M) repp(I, 1, M){int A, B; cin >> A >> B; v[A].pb(B), v[B].pb(A);}
#define in_dedges(M) repp(I, 1, M){int A, B; cin >> A >> B; v[A].pb(B);}
#define in_wedges(M) repp(I, 1, M){int A, B, C; cin >> A >> B >> C; v[A].pb(B, C), v[B].pb(A, C);}
#define in_dwedges(M) repp(I, 1, M){int A, B, C; cin >> A >> B >> C; v[A].pb(B, C);}
#define endl "\n"

const ll sp1 = 1e12 + 39; 
const ll sp2 = 1e15 + 1e5 + 11;
const ll INF = 1e18 + 18;
const ll MOD = 1e9 + 7;
const int inf = 2e9 + 9;
const size_t N = 1e6 + 6;

int main() //PointBlank's code ¯\_(ツ)_/¯
{
    quickio
    ull n;
    cin >> n;
 
    ull a = 3, b = 5;
    ull x = 1, y = 1;
    while(a < INF){
        b = 5, y = 1;
        while(b < INF){
            if(a + b == n){
                cout << x << " " << y;
                exit(0);
            }
            if(b * 5 < b) break;
            b *= 5, y++;
        }
        if(a * 3 < a) break;
        a *= 3, x++;
    }
    cout << -1;
}

ll power(ll x, ll y){ 
    ll res = 1; x %= MOD;
    while(y > 0){if(y & 1) res = (res*x) % MOD; y = y>>1, x = (x*x) % MOD;} 
    return res; 
}
#include<bits/stdc++.h>
using namespace std;
template<typename T>inline T read(){
	T f=0,x=0;char c=getchar();
	while(!isdigit(c)) f=c=='-',c=getchar();
	while(isdigit(c)) x=x*10+c-48,c=getchar();
	return f?-x:x;
}
#define int long long
namespace run{
	const int N=4e5+9;
	int head[N],nex[N],to[N],cnt;
	inline void add(int u,int v){
		nex[++cnt]=head[u];
		head[u]=cnt,to[cnt]=v;
	}

	int n,m,a[N],b[N],vis[N],tota,totb;
	inline void dfs(int u){
		vis[u]=1,tota+=a[u],totb+=b[u];
		for(int i=head[u];i;i=nex[i])
			if(!vis[to[i]]) dfs(to[i]);
	}
	int main(){
		n=read<int>(),m=read<int>();
		for(int i=1;i<=n;i++) a[i]=read<int>();
		for(int i=1;i<=n;i++) b[i]=read<int>();
		for(int i=1;i<=m;i++){
			int u=read<int>(),v=read<int>();
			add(u,v),add(v,u);
		}

		for(int i=1;i<=n;i++) if(!vis[i]){
			tota=totb=0,dfs(i);
			if(tota!=totb) puts("No"),exit(0);
		}
		puts("Yes");
		return 0;
	}
}
#undef int
int main(){
#ifdef my
	freopen(".in","r",stdin);
	freopen(".out","w",stdout);
#endif
	return run::main();
}