#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 200005
#define pb push_back
struct edge {
int v, c, i, d;
edge(int v, int c, int i, int d) : v(v), c(c), i(i), d(d) {}
};
typedef vector<vector<edge> > graph;
int n, m, d[MAXN], f[MAXN];
graph g;
int main() {
scanf("%d %d", &n, &m);
g = graph(n);
for(int i = 0; i < m; i++) {
int a, b, c; scanf("%d %d %d", &a, &b, &c);
a--; b--;
g[a].pb(edge(b,c,i,0));
g[b].pb(edge(a,c,i,1));
f[a] += c;
f[b] += c;
}
for(int i = 0; i < n; i++) {
f[i] /= 2;
}
memset(d, -1, sizeof(d));
queue<int> Q;
Q.push(0);
while(!Q.empty()) {
int u = Q.front(); Q.pop();
for(size_t i = 0; i < g[u].size(); i++) {
int id = g[u][i].i;
if(d[id] == -1) {
d[id] = g[u][i].d;
int v = g[u][i].v;
f[v] -= g[u][i].c;
if(v != n-1 && f[v] == 0) {
Q.push(v);
}
}
}
}
for(int i = 0; i < m; i++) {
printf("%d\n", d[i]);
}
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgTUFYTiAyMDAwMDUKI2RlZmluZSBwYiBwdXNoX2JhY2sKCnN0cnVjdCBlZGdlIHsKICAgIGludCB2LCBjLCBpLCBkOwoJZWRnZShpbnQgdiwgaW50IGMsIGludCBpLCBpbnQgZCkgOiB2KHYpLCBjKGMpLCBpKGkpLCBkKGQpIHt9Cn07Cgp0eXBlZGVmIHZlY3Rvcjx2ZWN0b3I8ZWRnZT4gPiBncmFwaDsKCmludCBuLCBtLCBkW01BWE5dLCBmW01BWE5dOwpncmFwaCBnOwoKaW50IG1haW4oKSB7CglzY2FuZigiJWQgJWQiLCAmbiwgJm0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