#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  long long int ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T>
inline T bpow(T p, T e) {
  long long int ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p);
    p = (p * p);
  }
  return (T)ret;
}
int toInt(string s) {
  int sm;
  stringstream ss(s);
  ss >> sm;
  return sm;
}
int toLlint(string s) {
  long long int sm;
  stringstream ss(s);
  ss >> sm;
  return sm;
}
struct D {
  int v, id, c;
  bool q;
  D() {}
  D(int b, int c, int d, bool e) : v(b), id(c), c(d), q(e) {}
};
vector<D> al[200005];
char rs[200005];
bool bl[200005];
int n, m, sz[200005];
long long int sm[200005], cr[200005];
int main() {
  int t, i, j, k;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &j, &k, &t);
    al[j].push_back(D(k, i, t, 0));
    al[k].push_back(D(j, i, t, 1));
    sz[j]++;
    sz[k]++;
    sm[j] += t;
    sm[k] += t;
    rs[i] = 3;
  }
  priority_queue<int> q;
  q.push(-1);
  bl[1] = 1;
  while (!q.empty()) {
    k = -q.top();
    q.pop();
    for (i = 0; i < sz[k]; i++) {
      if (rs[al[k][i].id] != 3) continue;
      cr[al[k][i].v] += al[k][i].c;
      rs[al[k][i].id] = al[k][i].q;
      if (cr[al[k][i].v] * 2 == sm[al[k][i].v]) {
        bl[al[k][i].v] = 1;
        q.push(-al[k][i].v);
      }
    }
  }
  for (i = 0; i < m; i++) {
    printf("%d\n", rs[i]);
  }
  return 0;
}
