def distance(route, graph):
  """
  Calculate total distance for a given route through a graph.

  Keyword arguments:
  route --- a list of node names that form the route
  graph --- a dictionary mapping node name pairs to distance
  """
  length = 0
  for i in range(1, len(route)):
    length += graph[(route[i - 1], route[i])]
  return length