#include <bits/stdc++.h>
#define ll long long
#define sum(a, b, mod) (((a) + (b)) % mod)
#define meow(cat...) fprintf(stderr, cat)
const ll MaxN = 2e5 + 10;
const ll inf = 2e9 + 7;
struct edge
{
ll next, to, flow;
} e[MaxN << 1];
ll n, m, s, t, id, sum, cnt, ans, ext[MaxN];
ll head[MaxN], cur[MaxN], dep[MaxN];
ll g[MaxN], c[MaxN], d[MaxN];
ll u[MaxN], v[MaxN], l[MaxN], r[MaxN];
void add(ll u, ll v, ll f)
{
++cnt;
e[cnt].to = v;
e[cnt].flow = f;
e[cnt].next = head[u];
head[u] = cnt;
}
void add_edge(ll u, ll v, ll f) { add(u, v, f), add(v, u, 0); }
inline ll read()
{
ll x = 0;
char ch = getchar();
while(ch > '9' || ch < '0')
ch = getchar();
while(ch <= '9' && ch >= '0')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x;
}
ll bfs()
{
std::queue<ll> q;
memset(dep, 0, sizeof(dep));
memcpy(cur, head, sizeof(head));
dep[s] = 1, q.push(s);
while(!q.empty())
{
ll u = q.front(); q.pop();
for(ll i = head[u]; i; i = e[i].next)
{
ll v = e[i].to, c = e[i].flow;
if(dep[v] || !c) continue;
dep[v] = dep[u] + 1, q.push(v);
}
}
return dep[t];
}
ll dinic(ll u, ll flow)
{
if(u == t) return flow;
ll rest = flow;
for(ll i = cur[u]; i && rest; i = e[i].next)
{
ll v = e[i].to, c = e[i].flow, k;
if(dep[v] == dep[u] + 1 && c)
{
cur[u] = i, k = dinic(v, std::min(c, rest));
if(k) e[i].flow -= k, e[i ^ 1].flow += k, rest -= k;
else dep[v] = -1;
}
}
if(rest) dep[u] = -1;
return flow - rest;
}
void add_edge(ll U, ll V, ll L, ll R)
{
++id, u[id] = U, v[id] = V, l[id] = L, r[id] = R;
add_edge(U, V, R - L), ext[V] += L, ext[U] -= L;
}
inline void solve()
{
ll now = 0;
while (bfs())
while ((now = dinic(s, inf)))
sum -= now;
}
void init()
{
cnt = 1, sum = ans = 0;
memset(ext, 0, sizeof(ext));
memset(head, 0, sizeof(head));
}
signed main()
{
while(scanf("%lld%lld", &n, &m) != EOF)
{
ll now = 0; init(), cnt = 1;
ll S = n + m + 1, T = n + m + 2;
for(ll i = 1; i <= m; i++)
g[i] = read(), add_edge(i + n, T, g[i], inf);
for(ll i = 1; i <= n; i++)
{
c[i] = read(), d[i] = read();
add_edge(S, i, 0, d[i]);
for(ll j = 1; j <= c[i]; j++)
{
ll id = read(), l = read(), r = read();
add_edge(i, id + n + 1, l, r);
}
}
s = n + m + 3, t = n + m + 4;
for (ll i = 1; i <= T; i++)
{
if (ext[i] > 0)
ans += ext[i], add_edge(s, i, ext[i]);
else if (ext[i] < 0) add_edge(i, t, -ext[i]);
}
add_edge(T, S, inf), sum = ans, solve(), ans = e[cnt].flow;
if (sum) { puts("-1\n"); continue; }
head[S] = e[head[S]].next, head[T] = e[head[T]].next, s = S, t = T;
while (bfs()) while ((now = dinic(s, inf))) ans += now;
printf("%lld\n\n", ans);
}
return 0;
}