#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 200010, M = N << 1, K = 17;
int n, idx, root = 1;
int h[N], e[M], ne[M], ids[M], times[M];
int depth[N], fa[N][K], din[N], ans[N];
LL c1[M], c2[M];
// id 记录这条边从哪里来的
void add(int a, int b, int id)
{
e[idx] = b, ids[idx] = id, ne[idx] = h[a], h[a] = idx++;
}
void bfs()
{
memset(depth, 127, sizeof(depth));
queue<int> q;
depth[0] = 0;
depth[root] = 1;
q.push(root);
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (depth[j] > depth[t] + 1)
{
depth[j] = depth[t] + 1;
q.push(j);
fa[j][0] = t;
for (int k = 1; k < K; k++)
{
fa[j][k] = fa[fa[j][k - 1]][k - 1];
}
}
}
}
}
// 计算两个节点的最近公共祖先 LCA
int lca(int a, int b)
{
if (depth[a] < depth[b])
swap(a, b);
for (int k = K - 1; k >= 0; k--)
{
if (depth[fa[a][k]] >= depth[b])
a = fa[a][k];
}
if (a == b)
return a;
for (int k = K - 1; k >= 0; k--)
{
if (fa[a][k] != fa[b][k])
{
a = fa[a][k];
b = fa[b][k];
}
}
return fa[a][0];
}
// DFS 计算每条边的使用次数
void dfs(int u, int father)
{
for (int i = h[u]; i != -1; i = ne[i])
{
int j = e[i];
if (j == father)
continue;
dfs(j, u);
times[u] += times[j];
ans[ids[i]] = times[j]; // 求出每一个点的值
}
}
int main()
{
cin >> n;
memset(h, -1, sizeof(h));
for (int i = 1; i < n; i++)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
add(a, b, i);
add(b, a, i);
c1[i] = c;
c2[i] = d;
}
bfs();
for (int i = 1; i < n; i++)
{
int p = lca(i, i + 1);
times[i]++;
times[i + 1]++;
times[p] -= 2;
}
dfs(1, -1);
// for (int i = 1; i < n; i++)
// {
// cout << ans[i] << " ";
// }
// 计算总成本
LL totalCost = 0;
for (int i = 1; i < n; i++)
{
totalCost += min(c1[i] * ans[i], c2[i]);
}
cout << totalCost << endl;
return 0;
}