import java.util.*

data class Rail(val to: Int, val cost: Long)
data class Station(val id:Int) {
    private val mRails: MutableCollection<Rail> = Stack()
    val rails: Iterable<Rail>
        get() = mRails
    fun addRail(to: Int, cost:Int): Unit {
        mRails.add(Rail(to, cost.toLong()))
    }
}
fun calMinCostFrom(from: Int, stations: Array<Station>): LongArray {
    val res = LongArray(stations.size){ Long.MAX_VALUE}.apply { this[from] = 0L }
    val queue = PriorityQueue<Pair<Int, Long>>(compareBy(Pair<Int, Long>::second)).apply { add(from to 0L) }
    while (queue.isNotEmpty()) {
        if (res[queue.peek().first] == queue.peek().second) {
            val (to, current) = queue.poll()
            for ((next, cost) in stations[to].rails) {
                if (res[next] > current + cost) {
                    res[next] = current + cost
                    queue.add(next to res[next])
                }
            }
        }else queue.poll()
    }
    return res
}
class MutablePair<A, B>(var first:A, var second:B)
fun calMinPath(t:Int, minFromS: LongArray, minFromU: LongArray, minFromV: LongArray, stations: Array<Station>): Long {
    val memo = Array(stations.size){ MutablePair(minFromU[it], minFromV[it])}
    val isSearching = BooleanArray(stations.size){false}.apply{this[t] = true}
    val queue = PriorityQueue<Pair<Int, Long>>(compareByDescending(Pair<Int, Long>::second)).apply { add(t to minFromS[t]) }
    while (queue.isNotEmpty()){
        val (to, current) = queue.poll()
        for ((next, cost) in stations[to].rails) {
            if (minFromS[next] == current - cost) {
                if (memo[next].first > memo[to].first) memo[next].first = memo[to].first
                if (memo[next].second > memo[to].second) memo[next].second = memo[to].second
                if (!isSearching[next]){
                    isSearching[next] = true
                    queue.add(next to minFromS[next])
                }
            }
        }
    }
    return minOf(minFromU.withIndex().map{(i, l) -> l + memo[i].second}.min()!!, minFromV.withIndex().map{(i, l) -> l + memo[i].first}.min()!!)
}
fun main(args:Array<String>): Unit {
    val (n, m) = readLine()!!.trim().split(' ').map(String::toInt)
    val (s, t) = readLine()!!.trim().split(' ').map(String::toInt)
    val (u, v) = readLine()!!.trim().split(' ').map(String::toInt)
    val stations = Array(n){Station(it)}
    repeat(m){
        val (a, b, c) = readLine()!!.trim().split(' ').map(String::toInt)
        stations[a - 1].addRail(b - 1, c)
        stations[b - 1].addRail(a - 1, c)
    }
    val minCostFromS = calMinCostFrom(s - 1, stations)
    val minCostFromU = calMinCostFrom(u - 1, stations)
    val minCostFromV = calMinCostFrom(v - 1, stations)
    println(calMinPath(t - 1, minCostFromS, minCostFromU, minCostFromV, stations))
}
