#include <bits/stdc++.h>
using namespace std;
int n, m;
const int MAXN = 2 * 100 * 1000 + 10;
struct EDGE {
  int y, w, ind;
  bool flag;
};
vector<EDGE> v[MAXN];
bool mark[MAXN];
int sum[MAXN][3];
int a[MAXN];
void DFS(int x) {
  if (mark[x]) return;
  if (sum[x][0] / 2 != sum[x][1]) return;
  mark[x] = true;
  for (int i = 0; i < (int)v[x].size(); i++) {
    int yy = v[x][i].y;
    int index = v[x][i].ind;
    int ww = v[x][i].w;
    if (!a[index]) {
      a[index] = v[x][i].flag + 1;
      sum[yy][1] += ww;
    }
  }
  for (int i = 0; i < (int)v[x].size(); i++) {
    int yy = v[x][i].y;
    if (sum[yy][0] == 2 * sum[yy][1]) DFS(yy);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int xx, yy, ww;
    EDGE edge1, edge8;
    cin >> xx >> yy >> ww;
    xx--, yy--;
    edge1.y = yy, edge1.w = ww, edge1.flag = 0, edge1.ind = i;
    edge8.y = xx, edge8.w = ww, edge8.flag = 1, edge8.ind = i;
    sum[xx][0] += ww;
    sum[yy][0] += ww;
    v[xx].push_back(edge1);
    v[yy].push_back(edge8);
    if (xx == n - 1) {
      a[i] = 2;
    }
    if (yy == n - 1) a[i] = 1;
  }
  sum[0][1] = sum[0][0] = 0;
  sum[n - 1][1] = -1;
  DFS(0);
  for (int i = 0; i < m; i++) cout << a[i] - 1 << endl;
  return 0;
}
