class Highway(object):
    def __init__(self, a = 0, b = 0, c = 0, d = 0):
        if isinstance(a,list):
            d = a[3]
            c = a[2]
            b = a[1]
            a = a[0]
        self.root = [int(a),int(b)]
        self.atob = int(c)
        self.btoa = int(d)

    def getRoot(self):
        return self.root

    def hasTown(self, n):
        if (self.root[0] is n) or (self.root[1] is n):
            return True
        else:
            return False

    def getNextAndCost(self, n):
        if self.root[0] is n:
            return [self.root[1], self.atob]
        elif self.root[1] is n:
            return [self.root[0], self.btoa]
        else:
            return [-1,-1]


class Town(object):
    def __init__(self, town = -1, highwayList = []):
        self.town = town
        self.path = []
        self.cost = -1
        self.done = False

        for n in highwayList:
            if n.hasTown(town):
                self.path.append(n.getNextAndCost(town))

    def __str__(self):
        text = "Town: " + str(self.town) + "\nCost: " + str(self.cost) + "\n"
        
        for i in self.path:
            text += "\tNext: " + str(i[0]) + " | Cost: " + str(i[1]) + "\n" 
        return  text

    def getTown(self):
        return self.town

    def getPath(self):
        return self.path

    def hasDone(self):
        return self.done

    def reset(self):
        self.cost = -1
        self.done = False

    def setCost(self, cost):
        self.cost = cost

    def getCost(self):
        return self.cost

    def decision(self):
        self.done = True

        
class Towns(object):
    def __init__(self, townN = 0, highway = []):
        self.townList = []
        for i in xrange(townN):
            self.townList.append(Town(i+1, highwayList))
        
    def __str__(self):
        text = ""
        for town in self.townList:
            text += str(town) 
        return text

    def getTown(self, n):
        for town in self.townList:
            if town.getTown() is n:
                return town
        return Town()

    def resetAll(self):
        for t in self.townList:
            t.reset()

    def trace(self, start, goal):
        self.resetAll()
        self.getTown(start).setCost(0)
        while True:
            doneTown = Town()
            for town in self.townList:
                if town.hasDone() or town.getCost() < 0:
                    continue
                if doneTown.getTown() == -1 or town.getCost() < doneTown.getCost():
                    doneTown = town

            if doneTown.getTown() == -1:
                break
            
            doneTown.decision()
            for p in doneTown.getPath():
                to = p[0]

                cost = doneTown.getCost() + p[1]
                if self.getTown(to).getCost() < 0 or cost < self.getTown(to).getCost():
                    self.getTown(to).setCost(cost)

        return self.getTown(goal).getCost()

townN = int(raw_input())
highwayN = int(raw_input())
highwayList = []

for i in xrange(highwayN):
    tmp = [int(h) for h in raw_input().split(",")]
    highwayList.append(Highway(tmp))

carpenter = [int(n) for n in raw_input().split(",")]

towns = Towns(townN,highwayList)

cost = towns.trace(carpenter[0], carpenter[1]) + towns.trace(carpenter[1], carpenter[0])

print carpenter[2] - carpenter[3] -cost

Measured memory_bytes: 4468