#include <bits/stdc++.h>
using namespace std;
const int N = 300100;
struct nd {
  int a, b, l;
} e[N], c[N];
vector<nd> vec[4 * N];
struct sta {
  int x, y, r, sum;
} s[N];
struct dui {
  int vl, id;
} a[N];
int fa[N], sum[N], rem, ans[N], n, m, b[N], top, t, le[4 * N], ri[4 * N];
bool cmp(nd x, nd y) { return x.l < y.l; }
bool cmp2(dui x, dui y) { return x.vl < y.vl; }
int find(int x) {
  if (fa[x] != x) return find(fa[x]);
  return x;
}
void unionn(int x, int y) {
  x = find(x);
  y = find(y);
  if (sum[x] > sum[y]) swap(x, y);
  s[++t] = (sta){x, y, rem, sum[x]};
  fa[x] = y;
  if ((sum[x] & 1) && (sum[y] & 1)) rem -= 2;
  sum[y] += sum[x];
}
void ret() {
  rem = s[t].r;
  sum[s[t].x] = s[t].sum;
  sum[s[t].y] -= s[t].sum;
  fa[s[t].x] = s[t].x;
  t--;
}
int bin(int rt, int vl) {
  int l = 0, r = vec[rt].size() - 1, mid, p = r + 1;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (vec[rt][mid].l >= vl)
      p = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return p;
}
void query(int rt, int l, int r, int qr, int vl, int vr) {
  if ((!l) || (!r) || (l > r)) return;
  if (!qr) return;
  if (r <= qr) {
    int p = bin(rt, vl);
    for (int i = p; ((i < vec[rt].size()) && (vec[rt][i].l <= vr)); i++)
      if (find(vec[rt][i].a) != find(vec[rt][i].b))
        unionn(vec[rt][i].a, vec[rt][i].b);
    return;
  }
  query(rt << 1, le[rt << 1], ri[rt << 1], qr, vl, vr);
  if (qr >= le[rt << 1 | 1])
    query(rt << 1 | 1, le[rt << 1 | 1], ri[rt << 1 | 1], qr, vl, vr);
}
void sol(int vl, int vr, int l, int r, int rt) {
  if (l > r) return;
  le[rt] = l;
  ri[rt] = r;
  if (vl == vr) {
    for (int i = l; i <= r; i++) ans[i] = vl;
    for (int i = l; i <= r; i++) c[i - l + 1] = e[i];
    sort(c + 1, c + r - l + 2, cmp);
    for (int i = 1; i <= r - l + 1; i++) vec[rt].push_back(c[i]);
    return;
  }
  int i, mid = (vl + vr) >> 1, la = t, la2, j;
  query(1, 1, m, l - 1, vl, mid);
  la2 = t;
  for (i = l; ((rem) && (i <= r)); i++) {
    if ((e[i].l <= mid) && (find(e[i].a) != find(e[i].b)))
      unionn(e[i].a, e[i].b);
    if (!rem) break;
  }
  while (t > la2) ret();
  sol(mid + 1, vr, l, i - 1, rt << 1);
  while (t > la) ret();
  for (j = l; j < i; j++)
    if ((e[j].l < vl) && (find(e[j].a) != find(e[j].b))) unionn(e[j].a, e[j].b);
  sol(vl, mid, i, r, rt << 1 | 1);
  while (t > la) ret();
  for (i = l; i <= r; i++) c[i - l + 1] = e[i];
  sort(c + 1, c + r - l + 2, cmp);
  for (i = 1; i <= r - l + 1; i++) vec[rt].push_back(c[i]);
}
int main() {
  int i, j, k;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d%d", &e[i].a, &e[i].b, &e[i].l);
    a[i].vl = e[i].l;
    a[i].id = i;
  }
  sort(a + 1, a + m + 1, cmp2);
  top = 0;
  for (i = 1; i <= m; i++) {
    if (a[i].vl != a[i - 1].vl) top++;
    e[a[i].id].l = top;
    b[top] = a[i].vl;
  }
  for (i = 1; i <= n; i++) {
    fa[i] = i;
    sum[i] = 1;
    rem = n;
  }
  t = 0;
  sol(1, top, 1, m, 1);
  for (i = 1; i <= n; i++) {
    fa[i] = i;
    sum[i] = 1;
    rem = n;
  }
  for (i = 1; i <= m; i++) {
    if (find(e[i].a) != find(e[i].b)) unionn(e[i].a, e[i].b);
    if (!rem) break;
    printf("-1\n");
  }
  for (; i <= m; i++) printf("%d\n", b[ans[i]]);
  return 0;
}
