// Arup Guha
// 4/8/2015
// Solution to 2015 NAIPC Problem H: Vending Machine

import java.util.*;
import java.io.*;

public class vending {

	public static void main(String[] args) throws Exception {

		// Read in snacks.
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(stdin.readLine().trim());
		item[] snacks = new item[n];
		for (int i=0; i<n; i++) {
			StringTokenizer tok = new StringTokenizer(stdin.readLine());
			int map = Integer.parseInt(tok.nextToken()) - 1;
			long buy = Long.parseLong(tok.nextToken());
			long sell = Long.parseLong(tok.nextToken());
			long quantity = Long.parseLong(tok.nextToken());
			snacks[i] = new item(map, buy, sell, quantity);
		}

		// Stores which buttons can be pressed to obtain item i.
		ArrayList[] into = new ArrayList[n];
		for (int i=0; i<n; i++)
			into[i] = new ArrayList<pair>();

		// Item and profit for pressing button i, update if we can make money.
		for (int i=0; i<n; i++) {
			int cur = snacks[i].getItem;
			long profit = snacks[cur].sell - snacks[i].buy;
			if (profit > 0) into[cur].add(new pair(i, profit));
		}

		// Sort each list, by order of profit.
		for (int i=0; i<n; i++) {
			Collections.sort(into[i]);
			if (into[i].size() > 0)
				snacks[((ArrayList<pair>)into[i]).get(0).index].isBestForward = true;
		}

		// Buy every profitable snack down to one.
		long res = 0;
		for (int i=0; i<n; i++)
			if (into[i].size() > 0)
				res += (((ArrayList<pair>)into[i]).get(0).profit*(snacks[i].quantity-1));

		// Find all cycles, you can buy everything except least expensive in a cycle.
		boolean[] used = new boolean[n];
		for (int i=0; i<n; i++) {
			if (into[i].size() > 0 && !used[i]) {

				long total = ((ArrayList<pair>)into[i]).get(0).profit;

				// What we stand to lose if we can't pick up this item in a cycle.
				long loss = getLoss((ArrayList<pair>)into[i]);
				used[i] = true;
				int next = i;
				boolean start = true;
				//boolean[] localUsed = new boolean[n];
				//localUsed[i] = true;

				// Look for cycle.
				while (true) {

					boolean isBest = snacks[next].isBestForward;
					next = snacks[next].getItem;

					// No cycle, so we can buy all!
					if (!isBest || (used[next] && next != i)) {
						loss = 0;
						break;
					}

					// This is a cycle - we lose nothing if its length 1.
					if (used[next]) {
						if (start) loss = 0;
						break;
					}

					// Mark it.
					used[next] = true;
					//localUsed[next] = true;
					total += ((ArrayList<pair>)into[next]).get(0).profit;
					loss = Math.min(loss, getLoss((ArrayList<pair>)into[next]));
					start = false;
				}

				// Add everything but (possibly) the lowest in this cycle.
				res += (total-loss);
			}
		}

		// This is all we can make.
		System.out.println(res);
	}

	public static long getLoss(ArrayList<pair> list) {
		if (list.size() == 1) return list.get(0).profit;
		return list.get(0).profit - list.get(1).profit;
	}
}

class item {

	public int getItem;
	public long buy;
	public long sell;
	public long quantity;
	public boolean isBestForward;

	public item(int index, long start, long end, long quan) {
		getItem = index;
		buy = start;
		sell = end;
		quantity = quan;
		isBestForward = false;
	}
}

class pair implements Comparable<pair> {

	public int index;
	public long profit;

	public pair(int i, long money) {
		index = i;
		profit = money;
	}

	public int compareTo(pair other) {
		if (this.profit > other.profit) return -1;
		if (this.profit < other.profit) return 1;
		return 0;
	}
}
