#include <bits/stdc++.h>
const int MAXNUM = 255000;
int n;
int m;
int ecnt;
int a[MAXNUM];
int b[MAXNUM];
int inflow[MAXNUM];
int outflow[MAXNUM], direct[MAXNUM];
int head[MAXNUM], to[MAXNUM * 2], next[MAXNUM * 2], c[MAXNUM * 2],
    from[MAXNUM * 2];
inline int readint() {
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  int x = 0;
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x;
}
inline void addEdge(int &u, int &v, int &i) {
  int x = readint();
  outflow[u] += x;
  outflow[v] += x;
  to[ecnt] = v;
  c[ecnt] = x;
  from[ecnt] = i;
  next[ecnt] = head[u];
  head[u] = ecnt++;
  to[ecnt] = u;
  c[ecnt] = x;
  from[ecnt] = i;
  next[ecnt] = head[v];
  head[v] = ecnt++;
}
void makeDirect() {
  std::stack<int> st;
  st.push(1);
  outflow[1] = 0;
  while (!st.empty()) {
    int u = st.top();
    st.pop();
    for (int p = head[u]; p; p = next[p]) {
      int &v = to[p];
      if (inflow[v] == outflow[v] && v != n) continue;
      inflow[v] += c[p];
      outflow[v] -= c[p];
      if (inflow[v] == outflow[v] && v != n) st.push(v);
      int x = from[p];
      if (v == a[x]) direct[x] = 1;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  ecnt = 1;
  for (int i = 0; i < m; ++i) {
    a[i] = readint();
    b[i] = readint();
    addEdge(a[i], b[i], i);
  }
  makeDirect();
  for (int i = 0; i < m; ++i) printf("%d\n", direct[i]);
}
