Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
d = dict()
l = []
for i in range(n):
  x,y = map(int, input().split())
  d[(x,y)] = i
  l.append((x,y))
road = [dict() for _ in range(n)]
l.sort()
for i in range(n-1):
  road[d[l[i]]][d[l[i+1]]] = l[i+1][0]-l[i][0]
  road[d[l[i+1]]][d[l[i]]] = l[i+1][0]-l[i][0]
l.sort(key=lambda x:x[1])
for i in range(n-1):
  if d[l[i+1]] in road[d[l[i]]]:
    road[d[l[i]]][d[l[i+1]]] = min(l[i+1][1]-l[i][1],road[d[l[i]]][d[l[i+1]]])
    road[d[l[i+1]]][d[l[i]]] = min(l[i+1][1]-l[i][1],road[d[l[i+1]]][d[l[i]]])
  else:
    road[d[l[i]]][d[l[i+1]]] = l[i+1][1]-l[i][1]
    road[d[l[i+1]]][d[l[i]]] = l[i+1][1]-l[i][1]

def prim(n, cost):
    mincost = [float("inf")]*n
    used = [False] * n
    mincost[0] = 0
    res = 0

    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1 or mincost[i] < mincost[v]):
                v = i
        if v == -1:
            break
        used[v] = True
        res += mincost[v]
        for i in cost[v]:
            mincost[i] = min(mincost[i],cost[v][i])
    return res

print(prim(n, road))


Fixed submission:
def find(u,x):
    if u[x]<0:
        return x
    u[x] = find(u,u[x])
    return u[x]

def unite(u,a,b):
    a = find(u,a)
    b = find(u,b)
    if a == b:
        return False
    if u[b] < u[a]:
        a,b = b,a
    u[a] += u[b]
    u[b] = a
    return True

def same(u,x,y):
    return find(u,x) == find(u,y)

n = int(input())
l = list()
for i in range(n):
  x,y = map(int,input().split())
  l.append((x,y,i))
l.sort()
uf = [-1]*(n+1)
ed = list()
for i in range(n-1):
  ed.append((l[i][2],l[i+1][2],l[i+1][0]-l[i][0]))
l.sort(key=lambda x:x[1])
for i in range(n-1):
  ed.append((l[i][2],l[i+1][2],l[i+1][1]-l[i][1]))
ed.sort(key=lambda x:x[2])
ans = 0
for x,y,d in ed:
  if not same(uf,x,y):
    unite(uf,x,y)
    ans += d
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
d = dict()
l = []
for i in range(n):
  x,y = map(int, input().split())
  d[(x,y)] = i
  l.append((x,y))
road = [dict() for _ in range(n)]
l.sort()
for i in range(n-1):
  road[d[l[i]]][d[l[i+1]]] = l[i+1][0]-l[i][0]
  road[d[l[i+1]]][d[l[i]]] = l[i+1][0]-l[i][0]
l.sort(key=lambda x:x[1])
for i in range(n-1):
  if d[l[i+1]] in road[d[l[i]]]:
    road[d[l[i]]][d[l[i+1]]] = min(l[i+1][1]-l[i][1],road[d[l[i]]][d[l[i+1]]])
    road[d[l[i+1]]][d[l[i]]] = min(l[i+1][1]-l[i][1],road[d[l[i+1]]][d[l[i]]])
  else:
    road[d[l[i]]][d[l[i+1]]] = l[i+1][1]-l[i][1]
    road[d[l[i+1]]][d[l[i]]] = l[i+1][1]-l[i][1]

def prim(n, cost):
    mincost = [float("inf")]*n
    used = [False] * n
    mincost[0] = 0
    res = 0

    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1 or mincost[i] < mincost[v]):
                v = i
        if v == -1:
            break
        used[v] = True
        res += mincost[v]
        for i in cost[v]:
            mincost[i] = min(mincost[i],cost[v][i])
    return res

print(prim(n, road))


Fixed submission:
def find(u,x):
    if u[x]<0:
        return x
    u[x] = find(u,u[x])
    return u[x]

def unite(u,a,b):
    a = find(u,a)
    b = find(u,b)
    if a == b:
        return False
    if u[b] < u[a]:
        a,b = b,a
    u[a] += u[b]
    u[b] = a
    return True

def same(u,x,y):
    return find(u,x) == find(u,y)

n = int(input())
l = list()
for i in range(n):
  x,y = map(int,input().split())
  l.append((x,y,i))
l.sort()
uf = [-1]*(n+1)
ed = list()
for i in range(n-1):
  ed.append((l[i][2],l[i+1][2],l[i+1][0]-l[i][0]))
l.sort(key=lambda x:x[1])
for i in range(n-1):
  ed.append((l[i][2],l[i+1][2],l[i+1][1]-l[i][1]))
ed.sort(key=lambda x:x[2])
ans = 0
for x,y,d in ed:
  if not same(uf,x,y):
    unite(uf,x,y)
    ans += d
print(ans)