#include <bits/stdc++.h>
using namespace std;
struct ji {
  int x, y, z;
} e[300005], ee[300005];
int n, m, tot, st[300005], f[300005], sz[300005], ans[300005];
bool cmp(ji x, ji y) { return e[x.z].z < e[y.z].z; }
int find(int k) {
  if (k == f[k]) return k;
  return find(f[k]);
}
void merge(int x, int y) {
  x = find(x), y = find(y);
  if (x != y) {
    if (sz[x] < sz[y]) swap(x, y);
    tot -= (sz[x] & 1) + (sz[y] & 1) - ((sz[x] + sz[y]) & 1);
    f[y] = x;
    sz[x] += sz[y];
    st[++st[0]] = y;
  }
}
void clear() {
  int y = st[st[0]--], x = f[y];
  f[y] = y;
  sz[x] -= sz[y];
  tot += (sz[x] & 1) + (sz[y] & 1) - ((sz[x] + sz[y]) & 1);
}
void dfs(int l, int r, int x, int y) {
  if (l > r) return;
  if (x == y) {
    for (int i = l; i <= r; i++)
      if (x == m)
        ans[i] = -1;
      else
        ans[i] = e[ee[x].z].z;
    return;
  }
  int s = (x + y >> 1), now = st[0], mid;
  for (int i = x + 1; i <= s; i++)
    if (ee[i].z <= l) merge(ee[i].x, ee[i].y);
  if (!tot)
    mid = l;
  else
    for (mid = l + 1; (tot) && (mid <= r); mid++)
      if (e[mid].z <= e[ee[s].z].z) {
        merge(e[mid].x, e[mid].y);
        if (!tot) break;
      }
  while (st[0] > now) clear();
  for (int i = x + 1; i <= s + 1; i++)
    if (ee[i].z <= l) merge(ee[i].x, ee[i].y);
  dfs(l, mid - 1, s + 1, y);
  while (st[0] > now) clear();
  for (int i = l + 1; i <= mid; i++)
    if (e[i].z <= e[ee[x].z].z) merge(e[i].x, e[i].y);
  dfs(mid, r, x, s);
  while (st[0] > now) clear();
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
    ee[i] = e[i];
    ee[i].z = i;
  }
  e[++m] = ji{1, 1, 0x3f3f3f3f};
  ee[m] = ji{1, 1, m};
  sort(ee + 1, ee + m + 1, cmp);
  tot = n;
  for (int i = 1; i <= n; i++) {
    f[i] = i;
    sz[i] = 1;
  }
  if (ee[1].z == 1) merge(e[1].x, e[1].y);
  dfs(1, m - 1, 1, m);
  for (int i = 1; i < m; i++) printf("%d\n", ans[i]);
}
