#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n, m;
int sz[N], fa[N], cnt, rk[N];
int ans[N];
struct edge {
  int x, y, w, no;
} e[N];
bool cmp(edge a, edge b) { return a.w < b.w; }
int fi;
vector<edge> b[N * 4];
multiset<int> em;
int gf(int x) { return fa[x] == 0 ? x : gf(fa[x]); }
struct state {
  int x, y, w, is_rk_same;
};
stack<state> S;
void merge(edge e) {
  int x = gf(e.x), y = gf(e.y);
  if (rk[x] > rk[y]) swap(x, y);
  if (x != y) {
    em.insert(e.w);
    state a = (state){x, y, e.w, rk[x] == rk[y]};
    S.push(a);
    fa[x] = y;
    if (rk[y] == rk[x]) rk[y]++;
    if ((sz[y] & 1) && (sz[x] & 1)) cnt -= 2;
    sz[y] += sz[x];
  } else
    S.push((state){0, 0, 0, 0});
}
void pop() {
  state a = S.top();
  S.pop();
  if (a.x == 0) return;
  sz[a.y] -= sz[a.x];
  fa[a.x] = 0;
  if (a.is_rk_same) rk[a.y]--;
  em.erase(em.find(a.w));
  if ((sz[a.y] & 1) && (sz[a.x] & 1)) cnt += 2;
}
void modify(int x, int l, int r, int tl, int tr, const edge &a) {
  if (l > tr || r < tl) return;
  if (tl <= l && r <= tr) {
    b[x].push_back(a);
    return;
  }
  modify(x << 1, l, l + r >> 1, tl, tr, a);
  modify(x << 1 | 1, (l + r >> 1) + 1, r, tl, tr, a);
}
void solve(int x, int l, int r) {
  for (edge t : b[x]) merge(t);
  if (l == r) {
    int lafi = fi;
    if (cnt != 0) {
      int i = fi + 1;
      for (; i <= m; i++) {
        if (cnt == 0) break;
        if (e[i].no <= l) {
          merge(e[i]);
          modify(1, 1, m, e[i].no, l - 1, e[i]);
        }
      }
      fi = i - 1;
    }
    if (cnt == 0) ans[l] = *em.rbegin();
    for (int i = fi; i > lafi; i--)
      if (e[i].no <= l) pop();
  } else {
    solve(x << 1 | 1, (l + r >> 1) + 1, r);
    solve(x << 1, l, l + r >> 1);
  }
  for (int i = 0; i < b[x].size(); i++) pop();
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y, w;
    scanf("%d %d %d", &x, &y, &w);
    e[i] = (edge){x, y, w, i};
  }
  sort(e + 1, e + 1 + m, cmp);
  for (int i = 1; i <= n; i++) sz[i] = 1;
  cnt = n;
  solve(1, 1, m);
  for (int i = 1; i <= m; i++)
    if (ans[i] == 0)
      printf("%d\n", -1);
    else
      printf("%d\n", ans[i]);
}
