#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1009;
struct Edges
{
	Edges(){}
	Edges(int x, char c):x(x), c(c){}
	int x;
	char c;
};
struct Vertices
{
	Vertices(){}
	Vertices(int x, int y, int d):x(x), y(y), d(d){}
	int x, y, d;
	bool operator < (const Vertices&a)
	const
	{
		return d > a.d;
	}
};
int n, m, d[N][N];
vector<Edges> g[N];
priority_queue<Vertices> q;
template<typename T>
T read()
{
	T num = 0; char c; bool flag = 0;
	while((c=getchar())==' ' || c=='\n' || c=='\r');
	if(c=='-') flag = 1; else num = c - '0';
	while(isdigit(c=getchar()))
			num = num * 10 + c - '0';
	return flag ? -num : num;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	n = read<int>(); m = read<int>();
	for(int i = 1; i <= m; i++)
	{
		int x = read<int>(), y = read<int>();
		char c;
		scanf("%c", &c);
		g[x].push_back(Edges(y, c));
		g[y].push_back(Edges(x, c));
	}
	memset(d, 0x3f, sizeof(d));
	q.push(Vertices(1, n, 0));
	d[1][n] = d[n][1] = 0;
	int ans = 2147483647;
	while(!q.empty())
	{
		Vertices now = q.top(); q.pop();
		for(int i = 0; i < g[now.x].size(); i++)
			for(int j = 0; j < g[now.y].size(); j++)
			{
				int x = g[now.x][i].x;
				int y = g[now.y][j].x;
				if(g[now.x][i].c!=g[now.y][j].c) continue;
				if((x==now.y) || (y==now.x))
					ans = min(ans, now.d+1);
				if(x==y)
					ans = min(ans, now.d+2);
				if(now.d+2>=d[x][y]) continue;
				d[x][y] = now.d + 2;
				d[y][x] = now.d + 2;
				q.push(Vertices(x, y, now.d+2));
			}
	}
	if(ans<2147483647)
		printf("%d\n", ans);
	else puts("-1");
	return 0;
}
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int n, m;
vector<ll> a, b;
vector<vector<int>> g;
vector<bool> check;

ll dfs(int v) {
	check[v] = true;
	ll ret = b[v] - a[v];
	for (auto nv : g[v]) {
		if (check[nv]) continue;
		ret += dfs(nv);
	}
	return ret;
}

int main() {
	cin >> n >> m;
	a.resize(n);
	b.resize(n);
	rep(i, n) cin >> a[i];
	rep(i, n) cin >> b[i];
	g.resize(n);
	rep(i, m) {
		int c, d;
		cin >> c >> d;
		c--, d--;
		g[c].push_back(d);
		g[d].push_back(c);
	}
	check.resize(n);
	bool ok = true;
	for (int i = 0; i < n; i++) {
		if (check[i]) continue;
		if (dfs(i) != 0) ok = false;
	}
	if (ok) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}