#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair< ll, ll > Pi;
using vl = vector<ll>;
using vs = vector<string>;
using vvl = vector<vector<ll>>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;
        
    }else{
        return a.second < b.second;
    }
}


bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};

class UnionFind {
    public:
        UnionFind() : _n(0) {}
        UnionFind(ll n) : _n(n), parent_or_size(n, -1) {}
        ll merge(ll a, ll b) {
            assert(0 <= a && a < _n);
            assert(0 <= b && b < _n);
            ll x = leader(a), y = leader(b);
            if (x == y) return x;
            if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
            parent_or_size[x] += parent_or_size[y];
            parent_or_size[y] = x;
            return x;
        }
        bool same(ll a, ll b) {
            assert(0 <= a && a < _n);
            assert(0 <= b && b < _n);
            return leader(a) == leader(b);
        }
        ll leader(ll a) {
            assert(0 <= a && a < _n);
            if (parent_or_size[a] < 0) return a;
            return parent_or_size[a] = leader(parent_or_size[a]);
        }
        ll size(ll a) {
            assert(0 <= a && a < _n);
            return -parent_or_size[leader(a)];
        }
        vvl groups() {
            vl leader_buf(_n), group_size(_n);
            rep (i, _n) {
                leader_buf[i] = leader(i);
                group_size[leader_buf[i]]++;
            }
            vvl result(_n);
            rep (i, _n) result[i].reserve(group_size[i]);
            rep (i, _n) result[leader_buf[i]].pb(i);
            result.erase(
            remove_if(all(result), [&](const vl& v) { return v.empty(); }), result.end());
            return result;
        }
    private:
        ll _n;
        // root node: -1 * component size
        // otherwise: parent
        std::vector<ll> parent_or_size;
};
ll N,M;
map<Pi,ll>m;
vvl G(2e5+5);
vector<ll>seen(2e5+5);
vl ans(2e5+5);
void dfs(ll v,ll p){
    seen[v] = 1;
    rep(i,G[v].size()){
        if(seen[G[v][i]] == 0){
            //cout<<
            if(m[{v,G[v][i]}] ==p){
                dfs(G[v][i],(p)%N+1);
                ans[G[v][i]]=(p)%N+1;
            }else{
                dfs(G[v][i],m[{v,G[v][i]}]);
                ans[G[v][i]]=m[{v,G[v][i]}];
            }
            
        }
    }
}
int main() {
    
    cin >>N>>M;
    UnionFind uf(N);
    
    rep(i,M){
        ll u,v,c;
        
        cin>>u>>v>>c;
        u--,v--;
        if(!uf.same(u,v)){
            G[v].pb(u);
            G[u].pb(v);
            m[{u,v}]=c;
            m[{v,u}]=c;
            uf.merge(u,v);
        }
    }
    dfs(0,1);
    ans[0]=1;
    rep(i,N){
        cout<<ans[i]<<endl;
    }


    return 0;
    

}
#include <bits/stdc++.h>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#define hh "\n"
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define mapa(a,b) make_pair(a,b)
#define mm(a,b) memset(a,b, sizeof(a));
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rof(i,a,b) for(int i=a;i>=b;i--)
#define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
template <typename T> T gcd (T a, T b) {while (b ^= a ^= b ^= a %= b); return a;}
inline char nc() {static char buf[1000000], *p1 = buf, *p2 = buf;return p1 == p2 && (p2 = (p1 = buf) + fread (buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;}
#define nc getchar
template <typename T> inline void read(T &sum) {char ch = nc();T tf = 0;sum = 0;while((ch < '0' || ch > '9') && (ch != '-')) ch = nc();tf = ((ch == '-') && (ch = nc()));while(ch >= '0' && ch <= '9') sum = sum * 10+ (ch - 48), ch = nc();(tf) && (sum = -sum);}
template <typename T> inline void write(T x){char F[200];T tmp=x>0?x:-x ;if(x<0)putchar('-') ;T cnt=0 ;while(tmp>0){F[cnt++]=tmp%10+'0';tmp/=10;}while(cnt>0)putchar(F[--cnt]) ;}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int main()
{
    IOS;
    int a,b,x,y;
    cin>>a>>b>>x>>y;
    if(a==b) cout<<x;
    else if(a>b)
    {
        if(x*2<=y) cout<<x+(a-b-1)*x*2;
        else cout<<x+(a-b-1)*y;
    }else
    {
        if(x*2<=y) cout<<x+(b-a)*x*2;
        else cout<<x+(b-a)*y;
    }
    return 0;
}
