#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int N = 200010;
const int inf = 1e9;
vector<pii> G[N];
int main()
{
/**
* Input format:
* first line: n m
* each hyperedge line: num_nodes weight node1 node2 node3 ... nodelast
* last line: s t
*/
int n, m;
scanf("%d%d", &n, &m);
// construct edges in hypergraph
for (int i = 0; i < m; ++i) {
int c, w;
scanf("%d%d", &c, &w);
while (c--) {
int v;
scanf("%d", &v);
G[v].push_back({n+i, w});
G[n+i].push_back({v, 0});
}
}
int s, t;
scanf("%d%d", &s, &t);
// normal dijkstra
vector<int> dist(n+m, inf);
vector<bool> visited(n+m, false);
dist[s] = 0;
priority_queue<pii, vector<pii>, greater<pii>> Q;
Q.push({dist[s], s});
while (!Q.empty()) {
int u = Q.top().second;
Q.pop();
if (visited[u])
continue;
visited[u] = true;
if (u == t)
break;
for (auto vw : G[u]) {
int v = vw.first, w = vw.second;
if (!visited[v] && dist[u]+w < dist[v]) {
dist[v] = dist[u]+w;
Q.push({dist[v], v});
}
}
}
printf("%d\n", visited[t] ? dist[t] : -1);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp1c2luZyBwaWkgPSBwYWlyPGludCwgaW50PjsKY29uc3QgaW50IE4gPSAyMDAwMTA7CmNvbnN0IGludCBpbmYgPSAxZTk7Cgp2ZWN0b3I8cGlpPiBHW05dOwoKaW50IG1haW4oKQp7CiAgICAvKioKICAgICAqIElucHV0IGZvcm1hdDoKICAgICAqIGZpcnN0IGxpbmU6IG4gbQogICAgICogZWFjaCBoeXBlcmVkZ2UgbGluZTogbnVtX25vZGVzIHdlaWdodCBub2RlMSBub2RlMiBub2RlMyAuLi4gbm9kZWxhc3QKICAgICAqIGxhc3QgbGluZTogcyB0CiAgICAgKi8KICAgIGludCBuLCBtOwogICAgc2NhbmYoIiVkJWQiLCAmbiwgJm0pOwogICAgLy8gY29uc3RydWN0IGVkZ2VzIGluIGh5cGVyZ3JhcGgKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgKytpKSB7CiAgICAgICAgaW50IGMsIHc7CiAgICAgICAgc2NhbmYoIiVkJWQiLCAmYywgJncpOwogICAgICAgIHdoaWxlIChjLS0pIHsKICAgICAgICAgICAgaW50IHY7CiAgICAgICAgICAgIHNjYW5mKCIlZCIsICZ2KTsKICAgICAgICAgICAgR1t2XS5wdXNoX2JhY2soe24raSwgd30pOwogICAgICAgICAgICBHW24raV0ucHVzaF9iYWNrKHt2LCAwfSk7CiAgICAgICAgfQogICAgfQogICAgaW50IHMsIHQ7CiAgICBzY2FuZigiJWQlZCIsICZzLCAmdCk7CgogICAgLy8gbm9ybWFsIGRpamtzdHJhCiAgICB2ZWN0b3I8aW50PiBkaXN0KG4rbSwgaW5mKTsKICAgIHZlY3Rvcjxib29sPiB2aXNpdGVkKG4rbSwgZmFsc2UpOwogICAgZGlzdFtzXSA9IDA7CiAgICBwcmlvcml0eV9xdWV1ZTxwaWksIHZlY3RvcjxwaWk+LCBncmVhdGVyPHBpaT4+IFE7CiAgICBRLnB1c2goe2Rpc3Rbc10sIHN9KTsKICAgIHdoaWxlICghUS5lbXB0eSgpKSB7CiAgICAgICAgaW50IHUgPSBRLnRvcCgpLnNlY29uZDsKICAgICAgICBRLnBvcCgpOwogICAgICAgIGlmICh2aXNpdGVkW3VdKQogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB2aXNpdGVkW3VdID0gdHJ1ZTsKICAgICAgICBpZiAodSA9PSB0KQogICAgICAgICAgICBicmVhazsKICAgICAgICBmb3IgKGF1dG8gdncgOiBHW3VdKSB7CiAgICAgICAgICAgIGludCB2ID0gdncuZmlyc3QsIHcgPSB2dy5zZWNvbmQ7CiAgICAgICAgICAgIGlmICghdmlzaXRlZFt2XSAmJiBkaXN0W3VdK3cgPCBkaXN0W3ZdKSB7CiAgICAgICAgICAgICAgICBkaXN0W3ZdID0gZGlzdFt1XSt3OwogICAgICAgICAgICAgICAgUS5wdXNoKHtkaXN0W3ZdLCB2fSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBwcmludGYoIiVkXG4iLCB2aXNpdGVkW3RdID8gZGlzdFt0XSA6IC0xKTsKICAgIAogICAgcmV0dXJuIDA7Cn0=