#include <bits/stdc++.h>   namespace modular {   const int MOD = 1000000007;   inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }   inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }   inline int mul(int x, int y) { return 1LL * x * y % MOD; }   inline int qpow(int x, int y) {     int ans = 1;     for (; y; y >>= 1, x = mul(x, x))       if (y & 1) ans = mul(ans, x);     return ans;   }   };  // namespace modular   namespace Base {   template <typename Tp>   inline Tp input() {     Tp x = 0, y = 1;     char c = getchar();     while ((c <  0  ||  9  < c) && c != EOF) {       if (c ==  - ) y = -1;       c = getchar();     }     if (c == EOF) return 0;     while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getchar();     return x *= y;   }   template <typename Tp>   inline void read(Tp &x) {     x = input<Tp>();   }   template <typename Tp>   inline void chmax(Tp &x, Tp y) {     x < y ? x = y : 0;   }   template <typename Tp>   inline void chmin(Tp &x, Tp y) {     x > y ? x = y : 0;   }   };  // namespace Base   using namespace Base;   int N, M;   struct edge {     int u, v, w, t;     bool operator<(const edge &rhs) const { return w < rhs.w; }   } e[400007];   struct UFS {     int fa[400007], sz[400007];     int top, cnt;     std::pair<int, int> st[400007];     inline void init() {       for (int i = 1; i <= N; ++i) fa[i] = i, sz[i] = 1;       top = 0;       cnt = N;     }     inline int find(int x) {       while (fa[x] != x) x = fa[x];       return x;     }     inline void merge(int u, int v) {       u = find(u), v = find(v);       if (u == v) return;       if (sz[u] < sz[v]) std::swap(u, v);       st[++top] = std::pair<int, int>(u, v);       if ((sz[u] & 1) && (sz[v] & 1)) cnt -= 2;       fa[v] = u, sz[u] += sz[v];     }     inline void pop(int t) {       while (top > t) {         int u = st[top].first, v = st[top].second;         top--;         sz[u] -= sz[v];         fa[v] = v;         if ((sz[u] & 1) && (sz[v] & 1)) cnt += 2;       }     }   } ufs;   int ans[400007];   struct segTree {     int pos;     std::vector<int> t[400007 << 2];     segTree() : pos(1) {}     void modi(int x, int l, int r, int ql, int qr, int id) {       if (ql <= l && r <= qr) return t[x].push_back(id), void();       int mid = l + r >> 1;       if (ql <= mid) modi(x << 1, l, mid, ql, qr, id);       if (mid < qr) modi(x << 1 | 1, mid + 1, r, ql, qr, id);     }     void solve(int x, int l, int r) {       int tmp = ufs.top;       for (int id : t[x]) ufs.merge(e[id].u, e[id].v);       if (l == r) {         for (; pos <= M && ufs.cnt; ++pos)           if (e[pos].t <= l) {             ufs.merge(e[pos].u, e[pos].v);             if (e[pos].t <= l - 1) modi(1, 1, M, e[pos].t, l - 1, pos);           }         ans[l] = ufs.cnt ? -1 : e[pos - 1].w;       } else {         int mid = l + r >> 1;         solve(x << 1 | 1, mid + 1, r);         solve(x << 1, l, mid);       }       ufs.pop(tmp);     }   } tr;   int main() {     read(N), read(M);     for (int i = 1; i <= M; ++i)       read(e[i].u), read(e[i].v), read(e[i].w), e[i].t = i;     std::sort(e + 1, e + M + 1);     ufs.init();     tr.solve(1, 1, M);     for (int i = 1; i <= M; ++i) printf( %d n , ans[i]);     return 0;   }