#include <iostream>
#include <queue>
#include <ctime>
#include <cassert>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <iomanip>
#define nextLine() { for (int c = getchar(); c != '\n' && c != EOF; c = getchar()); }
#define sqr(a) ((a)*(a))
#define has(mask,i) (((mask) & (1<<(i))) == 0 ? false : true)

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...)
#endif

#define TASK "secure"
using namespace std;

#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second

#if ( _WIN32 || __WIN32__ )
    #define LLD "%I64d"
#else
    #define LLD "%lld"
#endif

typedef long long LL;
typedef long double ldb;
typedef vector <int> vi;
typedef vector <vi> vvi;
typedef vector <bool> vb;
typedef vector <vb> vvb;

const int INF = 2000000000;
const ldb EPS = 1e-9;
const ldb PI = fabsl(atan2l(0.0, -1.0));
const int MAXN = 111111;

int n, m;
int type[MAXN];
vector <pii> g[MAXN];
void load() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &type[i]);
	}
	for (int i = 0, u, v, c; i < m; i++) {
		scanf("%d%d%d", &u, &v, &c);
		--u, --v;
		g[u].pb(mp(v, c));
		g[v].pb(mp(u, c));
	}
}

priority_queue <pii> q;
int d[MAXN];
bool was[MAXN];
int from[MAXN];

void solve() {
	for (int i = 0; i < n; i++) {
		if (type[i] == 1) {
			d[i] = 0;
			q.push(mp(0, i));
			from[i] = i;
		} else {
			d[i] = INF;
			from[i] = -1;
		}	
	}
	while (!q.empty()) {
		int v = q.top().se;
		q.pop();
		if (was[v]) {
			continue;
		}
		if (type[v] == 2) {
			printf("%d %d %d\n", from[v] + 1, v + 1, d[v]);
			return;
		}
		was[v] = true;
		for (int i = 0; i < (int)g[v].size(); i++) {
			int to = g[v][i].fi;
			int c = g[v][i].se;
			if (d[to] > d[v] + c) {
				d[to] = d[v] + c;
				q.push(mp(-d[to], to));
				from[to] = from[v];
			}
		}
	}
	puts("-1");
}

int main() {
	clock_t start = clock();
	freopen(TASK".in", "r", stdin);
	freopen(TASK".out", "w", stdout);
	load();
	solve();
	eprintf("time: %.3lf\n", (double)(clock() - start) / CLOCKS_PER_SEC);
	return 0;
}
