#include <bits/stdc++.h>
#define X first
#define Y second
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long int ll;
using ull = unsigned long long int;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pq = priority_queue<int>;
using ld = double;
constexpr int maxn = 3e5 + 10, mod = 998244353, K = 40;
constexpr ll inf = 1e18; int n, m, c[4105], len; char O[maxn]; ll b[75][75], a[75][75][4105];
void fwt(ll* f, int n, int op) {
for (int i = 1, p = 1; p <= len; i <<= 1, p++) {
if (O[p] == '|') for (int j = 0; j < n; j += i << 1) rep(k, 0, i - 1) f[i + j + k] = (f[i + j + k] + (op == 1 ? f[j + k] : mod - f[j + k])) % mod;
else if (O[p] == '&') for (int j = 0; j < n; j += i << 1) rep(k, 0, i - 1) f[j + k] = (f[j + k] + (op == 1 ? f[i + j + k] : mod - f[i + j + k])) % mod;
else for (int j = 0; j < n; j += i << 1) rep(k, 0, i - 1) {
int u = f[j + k], v = f[i + j + k];
f[j + k] = (u + v) % mod, f[i + j + k] = (u - v) % mod;
if (op == -1) f[j + k] = (ll)f[j + k] * 499122177 % mod, f[i + j + k] = (ll)f[i + j + k] % 499122177 % mod;
}
}
}
ll det() {
ll ans = 1;
rep(i, 1, n - 1) {
rep(j, i + 1, n - 1) if (!b[i][i] && b[j][i]) { ans = mod - ans; swap(b[i], b[j]); break; }
if (!b[i][i]) return 0;
rep(j, i + 1, n - 1) {
while (b[i][i]) {
ll w = b[j][i] / b[i][i];
rep(k, i, n - 1) b[j][k] = (b[j][k] - b[i][k] * w % mod + mod) % mod;
swap(b[i], b[j]); ans = mod - ans;
}
swap(b[i], b[j]); ans = mod - ans;
}
}
rep(i, 1, n - 1) ans = ans * b[i][i] % mod;
return ans;
}
int main() {
scanf("%d%d%s", &n, &m, O + 1); len = strlen(O + 1);
for (int i = 1, u, v, w; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
a[u][u][w] = (a[u][u][w] + 1) % mod, a[v][v][w] = (a[v][v][w] + 1) % mod;
a[u][v][w] = (a[u][v][w] + mod - 1) % mod, a[v][u][w] = (a[v][u][w] + mod - 1) % mod;
}
rep(i, 1, n - 1) rep(i, 1, n - 1) fwt(a[i][i], 1 << len, 1);
rep(w, 0, 1 << len) {
rep(i, 1, n) rep(j, 1, n) b[i][j] = a[i][j][w];
c[w] = det();
}
fwt(c, 1 << len, -1);
per(i, (1 << len) - 1, 0) if (c[i]) printf("%d\n", i), exit(0);
puts("-1");
return 0;
}