#include <bits/stdc++.h>
using namespace std;
inline int ___INT() {
  int ret;
  scanf("%d", &ret);
  return ret;
}
struct Edge {
  int u, v, w;
  int dir;
  Edge() {
    dir = -1;
    w = 0;
  }
  Edge(int _u, int _v) {
    u = _u;
    v = _v;
    w = 0;
    dir = -1;
  }
};
int nodeCount, edgeCount;
vector<int> adj[200005];
Edge E[200005];
int totalW[200005], totalF[200005];
void updateDir(int fromNode, int e) {
  if (E[e].u == fromNode)
    E[e].dir = 0;
  else
    E[e].dir = 1;
}
int getAdjNode(int u, int e) {
  if (E[e].u == u) return E[e].v;
  return E[e].u;
}
int calcDiff(int w, int f) { return abs(w - f - f); }
void updateNodeInPQ(int u, int newF, set<pair<int, int> >& PQ) {
  int diff = calcDiff(totalW[u], totalF[u]);
  set<pair<int, int> >::iterator it = PQ.find(pair<int, int>(diff, u));
  if (it != PQ.end()) PQ.erase(it);
  PQ.insert(pair<int, int>(calcDiff(totalW[u], newF), u));
}
void solve() {
  set<pair<int, int> > PQ;
  for (int i = 0; i < (adj[1].size()); ++i) {
    int e = adj[1][i];
    updateDir(1, e);
    int v = getAdjNode(1, e);
    totalF[v] += E[e].w;
    PQ.insert(pair<int, int>(calcDiff(totalW[v], totalF[v]), v));
  }
  while (PQ.size() > 0) {
    pair<int, int> node = *(PQ.begin());
    PQ.erase(PQ.begin());
    int u = node.second;
    assert(node.first == 0 || u == nodeCount);
    for (int i = 0; i < (adj[u].size()); ++i) {
      int e = adj[u][i];
      if (E[e].dir != -1) continue;
      updateDir(u, e);
      int v = getAdjNode(u, e);
      updateNodeInPQ(v, totalF[v] + E[e].w, PQ);
      totalF[v] += E[e].w;
    }
  }
}
int main() {
  nodeCount = ___INT();
  edgeCount = ___INT();
  memset(totalW, (0), sizeof(totalW));
  memset(totalF, (0), sizeof(totalF));
  for (int e = 0; e < (edgeCount); ++e) {
    int u = ___INT();
    int v = ___INT();
    int w = ___INT();
    E[e].u = u;
    E[e].v = v;
    E[e].w = w;
    E[e].dir = -1;
    adj[u].push_back(e);
    adj[v].push_back(e);
    totalW[u] += E[e].w;
    totalW[v] += E[e].w;
  }
  solve();
  for (int e = 0; e < (edgeCount); ++e) {
    printf("%d\n", E[e].dir);
  }
  return 0;
}
