#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define sc second

const double pi = acos(-1.0);
const int size = 5000 + 10;
const int inf = 1000 * 1000 * 1000;

int dist[size];
int way[size][size];
bool used[size];
int from[size];
int tp[size];
int n, m;

int main() {
    freopen("secure.in", "r", stdin);
    freopen("secure.out", "w", stdout);

    cin >> n >> m;
    for (int i = 0; i < n; i++)
    	cin >> tp[i];
    for (int i = 0; i < n; i++)
    	for (int j = 0; j < n; j++)
    		way[i][j] = inf;
    int v, u, cost;
    for (int i = 0; i < m; i++) {
    	cin >> v >> u >> cost;
    	v--, u--;
    	way[v][u] = min(way[v][u], cost);
    	way[u][v] = way[v][u];
    } 
    for (int i = 0; i < n; i++) {
    	dist[i] = inf * (tp[i] != 1);
    	from[i] = i;
    }	
    while (true) {
    	int mx = -1;
    	for (int i = 0; i < n; i++)
    		if (!used[i] && (mx == -1 || dist[mx] > dist[i])) {
    			mx = i;
    		}
    	if (mx == -1)
       		break;
       	used[mx] = true;
       	for (int i = 0; i < n; i++)
       		if (dist[i] > dist[mx] + way[mx][i]) {
       			from[i] = from[mx];
       			dist[i] = dist[mx] + way[mx][i];
       		}
    }
    int bleft = -1, bright = -1, bans = inf;
    for (int i = 0; i < n; i++)
		if (tp[i] == 2 && dist[i] < bans) {
			bleft = from[i];
			bright = i;
			bans = dist[i];
		}    	
	if (bans == inf)
		cout << -1 << endl;
	else
		cout << bleft + 1 << ' ' << bright + 1 << ' ' << bans << endl;

    return 0;
}