CodeEff/ECCO huggingface dataset:
# Original solution
#!/usr/bin/env python3

def main():
    N, M = list(map(int, input().split()))
    ans = 1
    if M > 0:
        a, b = list(map(int, input().split()))
        groups = []
        groups.append({a, b})
        for _ in range(1, M):
            a, b = list(map(int, input().split()))
            flag = True
            for g in groups:
                if a in g:
                    g.add(b)
                    flag = False
                if b in g:
                    g.add(a)
                    flag = False
            if flag:
                groups.append({a, b})

        for g in groups:
            ans = max(ans, len(g))

    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
#!/usr/bin/env python3


class UnionFind():
    # 作りたい要素数nで初期化
    # 使用するインスタンス変数の初期化
    def __init__(self, n):
        self.n = n
        # root[x]<0ならそのノードが根かつその値が木の要素数
        # rootノードでその木の要素数を記録する
        self.root = [-1] * (n + 1)
        # 木をくっつける時にアンバランスにならないように調整する
        self.rnk = [0] * (n + 1)

    # ノードxのrootノードを見つける
    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            # ここで代入しておくことで、後の繰り返しを避ける
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    # 木の併合、入力は併合したい各ノード

    def Unite(self, x, y):
        # 入力ノードのrootノードを見つける
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        # すでに同じ木に属していた場合
        if(x == y):
            return
        # 違う木に属していた場合rnkを見てくっつける方を決める
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            # rnkが同じ（深さに差がない場合）は1増やす
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
    # xとyが同じグループに属するか判断

    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)

    # ノードxが属する木のサイズを返す
    def Count(self, x):
        return -self.root[self.Find_Root(x)]


def main():
    N, M = list(map(int, input().split()))
    UF = UnionFind(N)
    for _ in range(M):
        a, b = list(map(int, input().split()))
        UF.Unite(a, b)
    ans = 0
    for i in range(N):
        ans = max(ans, UF.Count(i + 1))
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
# Union find
# かっつぱさんの解説と公式のスライドみた
# https://www.youtube.com/watch?time_continue=916&v=zxor0DdwoXA&feature=emb_logo
# https://www.slideshare.net/chokudai/union-find-49066733

import sys
sys.setrecursionlimit(10 ** 9)

n, m = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(m)]

root = [-1]*n

def find(x):
    if root[x] < 0:
        return x
    else:
        root[x] = find(root[x])
        return root[x]

def unite(x, y):
    gx = find(x)
    gy = find(y)
    
    if gx == gy:
        return
      
    if root[gx] > root[gy]:
        gx, gy = gy, gx
        
    root[gx] += root[gy]
    root[gy] = gx

def size(x):
    x = find(x)
    return -root[x]

for a, b in AB:
    a -= 1
    b -= 1
    unite(a, b)

ans = 0
for i in range(n):
    ans = max(ans, size(i))
print(ans)

# Optimized solution
import sys
sys.setrecursionlimit(10**9)

n, m = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(m)]

root = [-1] * n

def find(x):
    if root[x] < 0:
        return x
    else:
        return find(root[x])

def unity(x, y):
    gx = find(x)
    gy = find(y)

    if gx == gy:
        return

    if root[gx] > root[gy]:
        gx, gy = gy, gx

    root[gx] += root[gy]
    root[gy] = gx

for a, b in AB:
    a -= 1
    b -= 1
    unity(a, b)

print((-min(root)))
# Original solution
class Union:
    def __init__(self, n):
        self.parents = [-1] * n
    
    def root(self, x):
        if self.parents[x] < 0:
            return x
        else:
            return self.root(self.parents[x])
    
    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    
n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]

uni = Union(n)
for a, b in ab:
    a -= 1
    b -= 1
    uni.unite(a, b)
print((-min(uni.parents)))

# Optimized solution
n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]

class Union:
    def __init__(self, n):
        self.parents = [-1] * n
    
    def root(self, x):
        if self.parents[x] < 0:
            return x
        else:
            return self.root(self.parents[x])
    
    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

uni = Union(n)
for a, b in ab:
    uni.unite(a-1, b-1)
print((-min(uni.parents)))
# Original solution
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
N,M = list(map(int,input().split()))
uf = UnionFind(N+1)
for i in range(M):
    A,B = list(map(int,input().split()))
    uf.union(A,B)
Group = uf.all_group_members()
A = uf.roots()
Answer = 0
for h in range(len(Group)):
    Answer = max(Answer,len(Group[A[h]]))
print(Answer)

# Optimized solution
N,M = list(map(int, input().split()))
ABs = [list(map(int, input().split())) for _ in range(M)]
par = [i for i in range(N+1)]
size = [1 for _ in range(N+1)]

def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x]) #経路圧縮
        return par[x]
def same(x,y):
    return find(x) == find(y)
def unite(x,y):
    x = find(x)
    y = find(y)
    if x == y:
        return 0
    par[x] = y
    size[y] = size[x] + size[y]
    size[x] = 0

for AB in ABs:
    unite(AB[0],AB[1])
print((max(size)))
# Original solution
class UnionFind:
  def __init__(self, n):
    self.parent = [-1] * n
    self.rank = [1] * n
    self.count = [1] * n

  def get_root(self, i):
    while self.parent[i] != -1:
      i = self.parent[i]
    return i

  def is_same_tree(self, i, j):
    return self.get_root(i) == self.get_root(j)

  def merge(self, i, j):
    i = self.get_root(i)
    j = self.get_root(j)
    if i == j:
      return

    if self.rank[i] > self.rank[j]:
      self.parent[j] = i
      self.rank[i] = max(self.rank[i], self.rank[j] + 1)
      self.count[i] += self.count[j]
    else:
      self.parent[i] = j
      self.rank[j] = max(self.rank[j], self.rank[i] + 1)
      self.count[j] += self.count[i]


n, m = list(map(int, input().split()))
uf = UnionFind(n)
for _ in range(m):
  a, b = [int(i) - 1 for i in input().split()]
  uf.merge(a, b)
print((max(uf.count)))


# Optimized solution
class UnionFind:
  def __init__(self, n):
    self.parent = [-1] * n
    self.rank = [1] * n
    self.count = [1] * n

  def get_root(self, i):
    if self.parent[i] < 0:
      return i
    else:
      self.parent[i] = self.get_root(self.parent[i])
      return self.parent[i]
    #while self.parent[i] != -1:
    #  i = self.parent[i]
    #return i

  def is_same_tree(self, i, j):
    return self.get_root(i) == self.get_root(j)

  def merge(self, i, j):
    i = self.get_root(i)
    j = self.get_root(j)
    if i == j:
      return

    if self.rank[i] > self.rank[j]:
      self.parent[j] = i
      self.rank[i] = max(self.rank[i], self.rank[j] + 1)
      self.count[i] += self.count[j]
    else:
      self.parent[i] = j
      self.rank[j] = max(self.rank[j], self.rank[i] + 1)
      self.count[j] += self.count[i]


n, m = list(map(int, input().split()))
uf = UnionFind(n)
for _ in range(m):
  a, b = [int(i) - 1 for i in input().split()]
  uf.merge(a, b)
print((max(uf.count)))
# Original solution
N = int(eval(input()))
A = [int(i) for i in input().split()] 
mod = 10**9 + 7

b = [0]*N 
b[0] = A[0]
for n in range(1,N):
    b[n] = A[n] ^ b[n-1]
#print('累積論理和',b)
m = max(b)
dp = [[0]*(m+1) for j in range(2)]
dp[0] = [1]*(m+1)
cnt = [0]*(m+1)
z = 0

for i in range(N):
    if b[i] == 0:
        z +=1
    dp[0][b[i]] += dp[1][b[i]]*(z-cnt[b[i]])
    dp[0][b[i]] %= mod
    dp[1][b[i]] += dp[0][b[i]]
    dp[1][b[i]] %= mod
    cnt[b[i]] = z


if b[N-1]:
    print((dp[0][b[N-1]]))
else:
    ans = pow(2, z-1, mod)
    for i in range(1,m+1):
        ans += dp[1][i]
        ans %= mod
    print(ans)

# Optimized solution
N = int(eval(input()))
A = [int(i) for i in input().split()] 
mod = 10**9 + 7

b = [0]*N 
b[0] = A[0]
for n in range(1,N):
    b[n] = A[n] ^ b[n-1]
#print('累積論理和',b)
m = max(b)
dp = [[0]*(m+1) for j in range(2)]
dp[0] = [1]*(m+1)
cnt = [0]*(m+1)
z = 0 #zero_count

for i in range(N):
    if b[i] == 0:
        z +=1
    dp[0][b[i]] += dp[1][b[i]]*(z-cnt[b[i]])
    dp[0][b[i]] %= mod
    dp[1][b[i]] += dp[0][b[i]]
    dp[1][b[i]] %= mod
    cnt[b[i]] = z
    #print('i=',i,',bi=',b[i],',dp=',dp,'zero',z)


if b[N-1]:
    print((dp[0][b[N-1]]))
else:
    ans = pow(2, z-1, mod) #2^(z-1)%mod
    for i in range(1,m+1):
        ans += dp[1][i]
        ans %= mod
    print(ans)
# Original solution
import sys
from collections import defaultdict
from itertools import accumulate
from operator import xor


def solve_0(acc):
    MOD = 10 ** 9 + 7
    cnt_0 = 0
    counts = defaultdict(list)
    last_0 = {}
    for a in acc:
        if a == 0:
            cnt_0 += 1
        else:
            cnt = counts[a]
            if len(cnt) == 0:
                cnt.append(1)
                last_0[a] = cnt_0
            else:
                btw_0 = cnt_0 - last_0[a]
                if btw_0 > 0:
                    cnt.append(btw_0)
                    cnt.append(1)
                    last_0[a] = cnt_0
                else:
                    cnt[-1] += 1

    ans = pow(2, cnt_0 - 1, MOD)
    for i, cnt in list(counts.items()):
        dp0 = 1
        dp1 = cnt[0]
        for c0, c1 in zip(cnt[1::2], cnt[2::2]):
            dp0 = (dp0 + dp1 * c0) % MOD
            dp1 = (dp1 + dp0 * c1) % MOD
        ans = (ans + dp1) % MOD

    return ans


def solve_1(acc):
    MOD = 10 ** 9 + 7
    common = acc[-1]
    dp0 = 1
    dp1 = 0
    for a in acc:
        if a == 0:
            dp0 = (dp0 + dp1) % MOD
        elif a == common:
            dp1 = (dp1 + dp0) % MOD
    return dp0


n = int(eval(input()))
aaa = list(map(int, input().split()))
if 'PyPy' in sys.version:
    acc = [0] * n
    t = 0
    for i in range(n):
        t = acc[i] = t ^ aaa[i]
else:
    acc = list(accumulate(aaa, func=xor))
# print(acc)
if acc[-1] == 0:
    print((solve_0(acc)))
else:
    print((solve_1(acc)))


# Optimized solution
from collections import defaultdict
from itertools import accumulate
from operator import xor


def solve(acc):
    MOD = 10 ** 9 + 7
    cnt0 = 0
    dp0 = defaultdict(lambda: 1)
    dp1 = defaultdict(lambda: 0)
    last_0 = defaultdict(lambda: 0)
    for a in acc:
        if a == 0:
            cnt0 += 1
        else:
            if a not in dp1:
                dp1[a] = 1
                last_0[a] = cnt0
            else:
                bw0 = cnt0 - last_0[a]
                if bw0 > 0:
                    dp0[a] = (dp0[a] + dp1[a] * bw0) % MOD
                    last_0[a] = cnt0
                dp1[a] = (dp1[a] + dp0[a]) % MOD

    if acc[-1] == 0:
        return (pow(2, cnt0 - 1, MOD) + sum(dp1.values())) % MOD
    else:
        return dp0[acc[-1]]


n = int(eval(input()))
aaa = list(map(int, input().split()))
acc = list(accumulate(aaa, func=xor))
print((solve(acc)))
# Original solution
from collections import defaultdict
def inpl(): return list(map(int, input().split()))
MOD = 10**9 + 7
N = int(eval(input()))
S = []

x = 0
for a in map(int, input().split()):
    x ^= a
    S.append(x)

A = defaultdict(int)
B = defaultdict(int)
L = defaultdict(int)
Z = 0

if S[-1] != 0:
    sr = S[-1]
    a, b = 0, 0
    for s in S:
        if s == sr:
            b =  (b + a + 1)%MOD
        elif s == 0:
            a = (a + b)%MOD
    print((a+1))
else:
    for s in S:
        if s == 0:
            Z += 1
        else:
            A[s] = (A[s] + (Z - L[s])*B[s])%MOD
            B[s] = (A[s] + B[s] + 1)%MOD
            L[s] = Z
    print(((sum(B.values()) + pow(2, Z-1, MOD))%MOD))

# Optimized solution
from collections import defaultdict
MOD = 10**9 + 7
N = int(eval(input()))
S = []
x = 0
for a in map(int, input().split()):
    x ^= a
    S.append(x)

A = defaultdict(int)
B = defaultdict(int)
L = defaultdict(int)
Z = 0

for s in S:
    if s == 0:
        Z += 1
    else:
        A[s] = (A[s] + (Z - L[s])*B[s])%MOD
        B[s] = (A[s] + B[s] + 1)%MOD
        L[s] = Z

if S[-1] != 0:
    print(((A[S[-1]] + 1)%MOD))
else:
    print(((sum(B.values()) + pow(2, Z-1, MOD))%MOD))
# Original solution
x,a,b=list(map(int,input().split()))
if(a-b>=0):
    print("delicious")
elif(abs(a-b) <= x):
    print("safe")
else:
    print("dangerous")

# Optimized solution
x,a,b=list(map(int,input().split()))

if(b<=a):
    print('delicious')
elif(a<b and b<=a+x):
    print('safe')
else:
    print('dangerous')
# Original solution
x,a,b = list(map(int,input().split()))

if b-a > x:
        print('dangerous')
elif b-a > 0:
        print('safe')
else:
        print('delicious')

# Optimized solution
x,a,b = list(map(int,input().split()))

if b-a<=0:
    print('delicious')
elif b-a<=x:
    print('safe')
else:
    print('dangerous')
# Original solution
x,a,b=list(map(int,input().split()))
if((a-b)>=0):
    print('delicious')

elif((b-a)<(x+1)):
    print('safe')

else:
    print('dangerous')

# Optimized solution
x,a,b=list(map(int,input().split()))
print((['delicious','safe','dangerous'][(a<b)+(x+a<b)]))
# Original solution
X,A,B=list(map(int,input().split()))
if A>=B:
	print("delicious")
elif A<B and B<=A+X:
	print("safe")
else:
	print("dangerous")
	

# Optimized solution
X,A,B=list(map(int,input().split()))
if B<=A:
	print("delicious")
elif B-A>X:
	print("dangerous")
else:
	print("safe")
# Original solution
X,A,B=list(map(int,input().split()))
if B-A <= X and B-A > 0:
    print("safe")
elif B-A <=0:
    print("delicious")
elif B-A > X:
    print("dangerous")

# Optimized solution
x,a,b = list(map(int,input().split()))
A= 1
syoumikigen = A + a
B = A + b
safe = syoumikigen + x
out = safe + 1
if B <= syoumikigen:
    print("delicious")
elif syoumikigen < B <= safe:
    print("safe")
else:
    print("dangerous")
# Original solution
def main():
    import sys
    input = sys.stdin.readline
    x, a, b = list(map(int, input().split()))
    if b - a <= 0:
        print("delicious")
    elif b - a > x:
        print("dangerous")
    elif b - a <= x:
        print("safe")


if __name__ == '__main__':
    main()

# Optimized solution
def slove():
    import sys
    input = sys.stdin.readline
    x, a, b = list(map(int, input().rstrip('\n').split()))
    if b - a <= 0:
        print("delicious")
    elif b - a <= x:
        print("safe")
    else:
        print("dangerous")


if __name__ == '__main__':
    slove()
# Original solution
x, a, b = [int(s) for s in input().split()]
print(("delicious" if b <= a else ("safe" if b <= a + x else "dangerous")))

# Optimized solution
x, a, b = [int(s) for s in input().split()]
if b <= a:
    ans = "delicious"
elif b <= a + x:
    ans = "safe"
else:
    ans = "dangerous"
print(ans)
# Original solution
x, a, b = list(map(int, input().split()))
if b - a <= 0:
    print('delicious')
else:
    if b -a <= x :
        print('safe')
    else:
        print('dangerous')

# Optimized solution
X, A, B = list(map(int, input().split()))

print(('delicious' if A >= B else ('safe' if (A + X) >= B else 'dangerous')))
# Original solution
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0539

"""
import sys
from sys import stdin
from bisect import bisect_right
input = stdin.readline



def main(args):
    while True:
        d = int(eval(input()))        #  ??°??¶????????????
        if d == 0:
            break
        n = int(eval(input()))        #  ??¬????????????????????°
        m = int(eval(input()))        #  ??¨????????°
        cw_pos = [int(eval(input())) for _ in range(n-1)]
        dests = [int(eval(input())) for _ in range(m)]

        cw_pos.append(0)
        cw_pos.append(d)
        cw_pos.sort()

        ccw_pos = [d-x for x in cw_pos]
        ccw_pos.sort()


        total_distance = 0
        for t in dests:
            if t == 0:
                continue
            i = bisect_right(cw_pos, t)
            a1 = min(t-cw_pos[i-1], cw_pos[i]-t)

            j = bisect_right(ccw_pos, d-t)
            a2 = min(d-t-ccw_pos[j-1], ccw_pos[j]-(d-t))

            ans = min(a1, a2)
            total_distance += ans

        print(total_distance)


if __name__ == '__main__':
    main(sys.argv[1:])
    

# Optimized solution
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0539
AC
"""
import sys
from sys import stdin
from bisect import bisect_right
input = stdin.readline


def main(args):
    while True:
        d = int(eval(input()))        #  ??°??¶????????????
        if d == 0:
            break
        n = int(eval(input()))        #  ??¬????????????????????°
        m = int(eval(input()))        #  ??¨????????°
        cw_pos = [int(eval(input())) for _ in range(n-1)]
        dests = [int(eval(input())) for _ in range(m)]

        cw_pos.append(0)        #  ??¬????????????
        cw_pos.append(d)        #  ??°??¶??????1??¨??????????????¬????????????(??????)
        cw_pos.sort()

#        ccw_pos = [d-x for x in cw_pos] #  ??°??¶?????????????????£????????????????????????
#        ccw_pos.sort()


        total_distance = 0
        for t in dests:
            if t == 0:          #  ?????????????????????0?????\??£????????????bisect_right?????¨????????????????????§??????
                continue
            i = bisect_right(cw_pos, t)          #  ????¨?????????§??????????????????
            a1 = min(t-cw_pos[i-1], cw_pos[i]-t) #  ?????????????????????????????¢

#            j = bisect_right(ccw_pos, d-t)       #  ???????¨?????????§???????????¨?????????
#            a2 = min(d-t-ccw_pos[j-1], ccw_pos[j]-(d-t))

#            ans = min(a1, a2)
            total_distance += a1 #  ?????????????????????????????°OK

        print(total_distance)


if __name__ == '__main__':
    main(sys.argv[1:])
# Original solution
# AOJ 0539: Pizza
# Python3 2018.7.1 bal4u

import bisect

while True:
	d = int(eval(input()))
	if d == 0: break
	n, m = int(eval(input())), int(eval(input()))
	p = [0] + [int(eval(input())) for i in range(n-1)] + [d]
	p.sort()
	ans = 0
	for i in range(m):
		k = int(eval(input()))
		if k > 0:
			t = bisect.bisect_left(p, k)
			ans += min(k-p[t-1], p[t]-k)
	print(ans)


# Optimized solution
# AOJ 0539: Pizza
# Python3 2018.7.1 bal4u

import sys
from sys import stdin
input = stdin.readline
import bisect

while True:
	d = int(eval(input()))
	if d == 0: break
	n, m = int(eval(input())), int(eval(input()))
	p = [0] + [int(eval(input())) for i in range(n-1)] + [d]
	p.sort()
	ans = 0
	for i in range(m):
		k = int(eval(input()))
		if k > 0:
			t = bisect.bisect_left(p, k)
			ans += min(k-p[t-1], p[t]-k)
	print(ans)
# Original solution
import bisect

def solve():
    n=int(input())
    m=int(input())
    dist=[0]+[int(input()) for _ in range(n-1)]+[d]
    q=[int(input()) for _ in range(m)]
    dist.sort();

    ans=0
    for i in q:
        if bisect.bisect_left(dist,i)==n:
            l=n-1
        else:
            l=bisect.bisect_left(dist,i)
            if dist[l]!=i:
                l-=1
        r=bisect.bisect_left(dist,i)
        ans+=min(i-dist[l],dist[r]-i)

    return ans

while 1:
    d=int(input())
    if d==0:break
    ans=solve()
    print(ans)

# Optimized solution
import bisect

def solve():
    n=int(input())
    m=int(input())
    dist=[0]+[int(input()) for _ in range(n-1)]+[d]
    q=[int(input()) for _ in range(m)]
    dist.sort();

    ans=0
    for i in q:
        l=bisect.bisect_left(dist,i)
        if dist[l]==i:continue
        l-=1
        r=bisect.bisect_left(dist,i)
        ans+=min(i-dist[l],dist[r]-i)

    return ans

while 1:
    d=int(input())
    if d==0:break
    ans=solve()
    print(ans)
# Original solution
def find(l, e, start, end):
	mid = (start+end)//2
	if (l[mid] == e):
		return (e, e)
	elif (l[mid] > e):
		if (l[mid-1] < e):
			return (l[mid-1], l[mid])
		return find(l, e, start, mid)
	elif (l[mid] < e):
		if (l[mid+1] > e):
			return (l[mid], l[mid+1])
		return find(l, e, mid, end)

def print_sum():
	d = int(input())
	if d == 0: return False
	n, m = int(input()), int(input())
	S = [int(input()) for _ in range(n-1)]
	K = [int(input()) for _ in range(m)]
	S = [0] + sorted(S) + [d]
	sum = 0
	for k in K:
		low, high = find(S, k, 0, len(S))
		sum += min(k-low, high-k)
	print(sum)
	return True

while (print_sum()):
	pass

# Optimized solution
def find(l, e):
	start, end = 0, len(l)
	while True:
		mid = (start+end)//2
		if l[mid] == e: 
			return (e, e)
		elif (l[mid] > e):
			if (l[mid-1] < e):
				return (l[mid-1], l[mid])
			end = mid
			continue
		elif (l[mid] < e):
			if (l[mid+1] > e):
				return (l[mid], l[mid+1])
			start = mid

def print_sum():
	d = int(input())
	if d == 0: return False
	n, m = int(input()), int(input())
	S = [int(input()) for _ in range(n-1)]
	K = [int(input()) for _ in range(m)]
	S = [0] + sorted(S) + [d]
	sum = 0
	for k in K:
		low, high = find(S, k)
		sum += min(k-low, high-k)
	print(sum)
	return True

while (print_sum()):
	pass