#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template struct Edge { int src, dst; CostType cost; explicit Edge(const int src, const int dst, const CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge& x) const { if (cost != x.cost) return cost < x.cost; return src != x.src ? src < x.src : dst < x.dst; } inline bool operator<=(const Edge& x) const { return !(x < *this); } inline bool operator>(const Edge& x) const { return x < *this; } inline bool operator>=(const Edge& x) const { return !(*this < x); } }; int main() { int n, m; cin >> n >> m; vector> graph(n); while (m--) { int u, v; cin >> u >> v; --u; --v; graph[u].emplace_back(v); graph[v].emplace_back(u); } vector> cards(n); vector sum(n); int ub = 4; REP(i, n) { int a, b, c; cin >> a >> b >> c; cards[i] = {a, b, c}; sort(ALL(cards[i])); sum[i] = a + b + c; chmax(ub, a + b + c + 1); } REP(i, n) sort(ALL(graph[i]), [&](int a, int b) -> bool { return sum[a] < sum[b]; }); int lb = max({sum[0], sum[graph[0].front()] - cards[0].back() + 1, 3}); assert(lb < ub); while (ub - lb > 1) { const int mid = (lb + ub) / 2; vector is_visited(n, -1), top3{mid - 2, 1, 1}; auto merge = [&](int i) -> void { for (int e : cards[i]) top3.emplace_back(e); sort(ALL(top3), greater()); top3.resize(3); }; is_visited[0] = is_visited[graph[0].front()] = 1; merge(0); merge(graph[0].front()); priority_queue, vector>, greater>> que; FOR(i, 1, graph[0].size()) { const int e = graph[0][i]; is_visited[e] = 0; que.emplace(sum[e], e); } for (int e : graph[graph[0].front()]) { if (e != 0) { is_visited[e] = 0; que.emplace(sum[e], e); } } while (!que.empty()) { const int i = que.top().second; que.pop(); if (accumulate(ALL(top3), 0) <= sum[i]) break; is_visited[i] = 1; merge(i); for (int e : graph[i]) { if (is_visited[e] == -1) { is_visited[e] = 0; que.emplace(sum[e], e); } } } (is_visited[n - 1] == 1 ? ub : lb) = mid; } cout << "1 1 " << ub - 2 << '\n'; return 0; }