#include <bits/stdc++.h>
using namespace std;
FILE* g_f_;
int g_s_;
char* g_g_;
template <class _T>
inline _T sqr(const _T& x) {
  return x * x;
}
template <class _T>
inline string tostr(const _T& a) {
  ostringstream os("");
  os << a;
  return os.str();
}
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-11;
int n, m;
vector<int> a[402400];
int e[402400][3];
signed long long c[402400];
int ans[402400];
bool u[402400];
set<pair<signed long long, int> > w;
int main() {
  cout << setiosflags(ios::fixed) << setprecision(10);
  (g_s_ = scanf("%d%d", &n, &m));
  memset((u), 0, sizeof(u));
  memset((c), 0, sizeof(c));
  for (int i = 0; i < (int)(m); i++) {
    for (int j = 0; j < (int)(3); j++) {
      (g_s_ = scanf("%d", &e[i][j]));
    }
    e[i][0]--;
    e[i][1]--;
  }
  for (int i = 0; i < (int)(m); i++) {
    e[i + m][0] = e[i][1];
    e[i + m][1] = e[i][0];
    e[i + m][2] = e[i][2];
  }
  for (int i = 0; i < (int)(m + m); i++) {
    a[e[i][0]].push_back(i);
  }
  memset(ans, 0xff, sizeof(ans));
  for (int i = 0; i < (int)(m); i++) {
    int x = e[i][0];
    int y = e[i][1];
    if (x == 0) {
      ans[i] = 0;
      c[y] -= e[i][2];
    } else if (y == 0) {
      ans[i] = 1;
      c[x] -= e[i][2];
    } else {
      c[x] += e[i][2];
      c[y] += e[i][2];
    }
  }
  w.clear();
  for (int i = (1); i <= (int)(n - 2); i++) {
    w.insert(make_pair(c[i], i));
  }
  u[0] = true;
  for (int i = 0; i < (int)(n - 2); i++) {
    pair<signed long long, int> tmp = *(w.begin());
    w.erase(w.begin());
    int v = tmp.second;
    if (tmp.first != 0) return 239;
    u[v] = true;
    for (int j1 = 0; j1 < (int)(a[v].size()); j1++) {
      int j = a[v][j1];
      int y = e[j][1];
      if (!u[y]) {
        if (j < m)
          ans[j] = 0;
        else
          ans[j - m] = 1;
        if (y != n - 1) {
          w.erase(make_pair(c[y], y));
          c[y] -= 2 * e[j][2];
          w.insert(make_pair(c[y], y));
        }
      }
    }
  }
  for (int i = 0; i < (int)(m); i++) {
    if (ans[i] == -1)
      for (;;)
        ;
    printf("%d\n", ans[i]);
  }
  return 0;
}
