def f(v, edges, r)
  mins = []
  edges.each{|s, t, w|
    mins[t] = [w, s] if mins[t] == nil || mins[t][0] > w
  }
  mins[r] = [0, -1]

  group = Array.new(v){0}
  comp = Array.new(v){0}
  cnt = 0

  used = {}

  v.times{|i|
    unless used[i] then
      chain = []
      cur = i
      while used[i] == nil && cur != -1
        chain << cur
        used[cur] = 1
        cur = mins[cur][1]
      end
      if cur != -1
        cycle = false
        chain.each{|e|
          group[e] = cnt
          if e == cur
            cycle = true
            comp[cnt] = 1
          end
          cnt += 1 unless cycle
        }
      else
        chain.each{|e|
          group[e] = cnt
          cnt += 1
        }
      end
    end
  }
  if cnt == v then
    sum = mins.map {|w, s| w}.inject(:+)
    return sum
  end

  res = 0
  v.times{|i|
    next if i == r
    if comp[group[i]] == 1
      res += mins[i][0]
    end
  }

  n_es = []
  ii = 0
  edges.each{|s, t, w|
    gs = group[s]
    gt = group[t]
    next if gs == gt
    if comp[gt]
      n_es[ii] = [gs, gt, w - mins[t][0]]
    else
      n_es[ii] = [gs, gt, w]
    end
    ii += 1
  }
  return res + f(cnt, n_es, group[r])
end

v, e, r = gets.split.map(&:to_i)
edges = []
e.times{|i|
  s, t, w = gets.split.map(&:to_i)
  edges[i] = [s, t, w]
}
puts f(v, edges, r)