//#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>
#define mp make_pair
#define pb push_back

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

#define TIMESTAMP(x) eprintf("["#x"] Time : %.3lf s.\n", clock()*1.0/CLOCKS_PER_SEC)
#define TIMESTAMPf(x,...) eprintf("[" x "] Time : %.3lf s.\n", __VA_ARGS__, clock()*1.0/CLOCKS_PER_SEC)

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

using namespace std;

#define TASKNAME "secure"

#ifdef LOCAL
static struct __timestamper {
	~__timestamper(){
        TIMESTAMP(end);
	}
} __TIMESTAMPER;
#endif

typedef long long ll;
typedef long double ld;

const int MAXN = 110000;

int d[MAXN];
int from[MAXN];
int ty[MAXN];

vector<int> g[MAXN];
vector<int> len[MAXN];


int main(){
    freopen(TASKNAME".in","r",stdin);
    freopen(TASKNAME".out","w",stdout);
    memset(d, 0x3f, sizeof(d));
    memset(from, -1, sizeof(d));
    int n, m;
    scanf("%d%d", &n, &m);

    for (int i = 0; i < n; i++)
       scanf("%d", &ty[i]);

    for (int i = 0; i < m; i++){
    	int a, b, c;
    	scanf("%d %d %d", &a, &b, &c);
    	--a,--b;
    	g[a].pb(b);len[a].pb(c);
    	g[b].pb(a);len[b].pb(c);
    }

    set< pair<int,int> > s;

    for (int i = 0; i < n; i++)
    	if (ty[i] == 1){
    	   s.insert(mp(0, i));
    	   d[i] = 0;
    	   from[i] = i;
    	}

    while (!s.empty()) {
    	int v = s.begin()->second;
    	s.erase(s.begin());
    	eprintf("%d\n", v);
    	if (ty[v] == 2){
    		printf("%d %d %d\n", from[v] + 1, v + 1, d[v]);
    		return 0;
    	}
    	for (int i = 0; i < (int)g[v].size(); i++)
    		if (d[g[v][i]] > d[v] + len[v][i]){
    		    s.erase(mp(d[g[v][i]], g[v][i]));
    		    d[g[v][i]] = d[v] + len[v][i];
    		    from[g[v][i]] = from[v];
    		    s.insert(mp(d[g[v][i]], g[v][i]));
    		}
    }
    printf("-1\n");      
    return 0;
}