class Edge
  include Comparable
  attr_accessor :start, :to, :weight, :bias
  def initialize(s = 0, t = 0, w = 0)
    @start = s
    @to = t
    @weight = w
    @bias = 0
  end
  def <=>(other)
    return (other.weight - other.bias) <=> (weight - bias)
  end
end

def main(input = STDIN)
  v,e,r = input.gets.split(" ").map(&:to_i)
  node = Array.new(v, false)
  edge_to = Array.new(v){[]}
  edge_from = Array.new(v){[]}
  e.times do
    s, t, w = input.gets.split(" ").map(&:to_i)
    edge_to[s] << Edge.new(s, t, w)
    edge_from[t] << edge_to[s].last
  end
  edge_from.each{|n|
    minw = 10000
    n.each{|e|
      minw = e.weight if minw > e.weight
    }
    n.each{|e|
      e.bias = minw
    }
  }
  sum = 0;
  node[r] = true
  queue = []
  edge_to[r].each{|e|
    queue.push(e)
  }
  (v - 1).times do
    queue.sort!
    while (node[queue.last.to])
      queue.pop
    end
    temp = queue.last
    queue.pop
    node[temp.to] = true
    sum += temp.weight
    edge_to[temp.to].each{|e|
      queue.push(e) unless node[e.to]
    }
  end
  puts sum
end
main