#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 10;
int n, m;
int x[300010], y[300010], z[300010], fa[300010], siz[300010];
int num;
int ans[300010], id[300010], p[300010];
vector<pair<pair<int, int>, int> > s;
inline int read() {
  int ans = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9')
    ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  return ans;
}
inline int cmp(int x, int y) { return z[x] < z[y]; }
inline int find(int x) {
  if (fa[x] == x) return x;
  return find(fa[x]);
}
void ins(int u) {
  int fx = find(x[u]), fy = find(y[u]);
  if (fx != fy) {
    if (siz[fx] > siz[fy]) swap(fx, fy);
    fa[fx] = fy;
    int k = siz[fx] & siz[fy] & 1;
    k <<= 1;
    num -= k;
    siz[fy] += siz[fx];
    s.push_back(make_pair(make_pair(fx, fy), k));
  }
}
void del(pair<pair<int, int>, int> u) {
  int fx = u.first.first, fy = u.first.second, fz = u.second;
  siz[fy] -= siz[fx];
  fa[fx] = fx;
  num += fz;
}
void solve(int l, int r, int dx, int dy) {
  if (l > r) return;
  int mid = (l + r) >> 1;
  int sizd = s.size();
  for (int i = l; i <= mid; i++)
    if (p[i] < dx) ins(i);
  for (int i = dx; i <= dy; i++)
    if (id[i] <= mid) {
      ins(id[i]);
      if (!num) {
        ans[mid] = i;
        break;
      }
    }
  while ((int)s.size() > sizd) del(s.back()), s.pop_back();
  if (ans[mid]) {
    for (int i = dx; i < ans[mid]; i++)
      if (id[i] < l) ins(id[i]);
    solve(l, mid - 1, ans[mid], dy);
    while ((int)s.size() > sizd) del(s.back()), s.pop_back();
    dy = ans[mid];
  }
  for (int i = l; i <= mid; i++)
    if (p[i] < dx) ins(i);
  solve(mid + 1, r, dx, dy);
  while ((int)s.size() > sizd) del(s.back()), s.pop_back();
}
int main() {
  n = read(), m = read();
  num = n;
  for (int i = 1; i <= n; i++) fa[i] = i, siz[i] = 1;
  for (int i = 1; i <= m; i++)
    x[i] = read(), y[i] = read(), z[i] = read(), id[i] = i;
  sort(id + 1, id + m + 1, cmp);
  for (int i = 1; i <= m; i++) p[id[i]] = i;
  solve(1, m, 1, m);
  for (int i = 1; i <= m; i++) printf("%d\n", !ans[i] ? -1 : z[id[ans[i]]]);
}
