#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010, MAXM = 300010;
int n, m;
int a[MAXM], b[MAXM], c[MAXM];
int f[MAXN], size[MAXN];
vector<pair<int &, int>> backups;
int odds, last;
int ans[MAXM];
int modify(int &a, int b) {
  backups.emplace_back(a, a);
  return a = b;
}
void rollback(size_t tm) {
  while (backups.size() > tm) {
    backups.back().first = backups.back().second;
    backups.pop_back();
  }
}
void init() {
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
  }
  for (int i = 1; i <= n; ++i) f[i] = i, size[i] = 1;
  odds = n, last = 0;
}
int find(int i) { return f[i] == i ? i : modify(f[i], find(f[i])); }
void add(int i) {
  int x = find(a[i]), y = find(b[i]);
  if (x == y) return;
  if (size[x] % 2 == 1 && size[y] % 2 == 1) modify(odds, odds - 2);
  if (c[i] > last) modify(last, c[i]);
  modify(f[y], x);
  modify(size[x], size[x] + size[y]);
}
bool by_c(int i, int j) { return c[i] < c[j]; }
template <typename Iter>
Iter adding(Iter first, Iter last) {
  while (first != last && odds) add(*first++);
  return first;
}
void solve(int l, int r, const vector<int> &others) {
  int t0 = backups.size();
  if (l == r) {
    vector<int> edges;
    for (int i : others)
      if (c[i] < c[l]) edges.push_back(i);
    edges.push_back(l);
    for (int i : others)
      if (c[i] >= c[l]) edges.push_back(i);
    adding(edges.begin(), edges.end());
    ans[l] = odds ? -1 : last;
  } else {
    for (int i = l; i <= r && odds; ++i) add(i);
    auto limits = adding(others.begin(), others.end());
    rollback(t0);
    adding(others.begin(), limits);
    int t1 = backups.size();
    vector<int> maybe(limits, adding(limits, others.end()));
    rollback(t1);
    int mid = (l + r) / 2;
    solve(l, mid, maybe);
    for (int i = l; i <= mid; ++i) maybe.push_back(i);
    sort(maybe.begin(), maybe.end(), by_c);
    solve(mid + 1, r, maybe);
  }
  rollback(t0);
}
void answer() {
  for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    init();
    solve(1, m, {});
    answer();
  }
}
