import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.DecimalFormat;
import java.lang.reflect.Array;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.nio.channels.ShutdownChannelGroupException;
public class A{
	public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
	static long MOD = (long)(1e9+7);
	static long MOD2 = MOD*MOD;
	//static long MOD = 998244353;
	static FastReader sc = new FastReader();
	static int pInf = Integer.MAX_VALUE;
	static int nInf = Integer.MIN_VALUE;
	public static void main(String[] args){
		int test = 1;
		//test = sc.nextInt();
		while(test-->0)solve();
		out.flush();
		out.close();
	}
	public static void solve() {
		int n = sc.nextInt();
		int k = sc.nextInt();
		Node[] nodes = new Node[n];
		for(int i = 0; i < n; i++) {
			nodes[i] = new Node(i+1);
		}
		for(int i = 0; i < n-1; i++) {
			int a = sc.nextInt()-1;
			int b = sc.nextInt()-1;
			nodes[a].adj.add(nodes[b]);
			nodes[b].adj.add(nodes[a]);
		}
		nodes[0].dfs0(null, 0);
		/*for(int i = 0; i < n; i++) {
			out.println(nodes[i].toString());
		}*/
		Arrays.sort(nodes);
		long ans = 0;
		int g = n-1;
		while(k-->0) {
			ans += (nodes[g].depth-nodes[g].subTreeSize+1);
			g--;
		}
		out.println(ans);
		return;
	}
	static class Node implements Comparable<Node>{
		int depth,subTreeSize,id;
		ArrayList<Node> adj = new ArrayList<>();
		public Node(int id) {
			this.id = id;
		}
		public void dfs0(Node par,int depth) {
			if(par!=null) {
				adj.remove(par);
			}
			this.depth = depth;
			this.subTreeSize = 1;
			for(Node nn:adj) {
				nn.dfs0(this, depth+1);
				subTreeSize += nn.subTreeSize;
			}
		}
		public int compareTo(Node o) {
			int value=depth-subTreeSize, oValue=o.depth-o.subTreeSize;
			return Integer.compare(value, oValue);
		}
		public String toString() {
			return "id: "+id+" depth: "+depth+" subtreeSize: "+subTreeSize;
		}
	}
	public static int lowerBound(int key, ArrayList<Integer> A) {
		int s = 0;
		int e = A.size()-1;
		if(e==-1) {
			return 0;
		}
		int ans = -1;
		while(s<=e) {
			int m = s+(e-s)/2;
			if(A.get(m)>=key) {
				ans = m;
				e = m-1;
			}
			else {
				s = m+1;
			}
		}
		return ans==-1?s:ans;
	}
	public static int upperBound(int key, ArrayList<Integer> A) {
		int s = 0;
		int e = A.size()-1;
		if(e==-1) {
			return 0;
		}
		int ans = -1;
		while(s<=e) {
			int m = s+(e-s)/2;
			if(A.get(m)>key) {
				ans = m;
				e = m-1;
			}
			else {
				s = m+1;
			}
		}
		return ans==-1?s:ans;
	}
	public static long c2(long n) {
		if((n&1)==0) {
			return mul(n/2, n-1);
		}
		else {
			return mul(n, (n-1)/2);
		}
	}
	public static long mul(long a, long b){
	    return ((a%MOD)*(b%MOD))%MOD;
	}
	public static long add(long a, long b){
	    return ((a%MOD)+(b%MOD))%MOD;
	}
	public static long sub(long a, long b){
	    return ((a%MOD)-(b%MOD))%MOD;
	}
//Pair Class
	static class Pair implements Comparable<Pair>{
		int x;
		int y;
		int len;
		public Pair(int x, int y) {
			this.x = x;
			this.y = y;
			this.len = y-x+1;
		}
		@Override
		public int compareTo(Pair o) {
			if(this.x==o.x){
			    return (this.y-o.y);
			}
			return -1*(this.x-o.x);
		}
	}
//nCr
	static long ncr(long n, long k) {
        long ret = 1;
        for (long x = n; x > n - k; x--) {
            ret *= x;
            ret /= (n - x + 1);
        }

        return ret;
    }
	static long finextDoubleMMI_fermat(long n,int M)
    {
        return fastExpo(n,M-2);
    }

    static long nCrModPFermat(int n, int r, int p)
    {
        if (r == 0)
            return 1;
        long[] fac = new long[n+1];
        fac[0] = 1;
        for (int i = 1 ;i <= n; i++)
            fac[i] = fac[i-1] * i % p;
        return (fac[n]* finextDoubleMMI_fermat(fac[r], p)% p * finextDoubleMMI_fermat(fac[n-r], p) % p) % p;
    }
//Shuffle Sort
    static final Random random=new Random();
	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
//Merge Sort
	static void merge(long arr[], int l, int m, int r)
    {
        // Find sizes of two subarrays to be merged
        int n1 = m - l + 1;
        int n2 = r - m;

        /* Create temp arrays */
        long L[] = new long [n1];
        long R[] = new long [n2];

        /*Copy data to temp arrays*/
        for (int i=0; i<n1; ++i)
            L[i] = arr[l + i];
        for (int j=0; j<n2; ++j)
            R[j] = arr[m + 1+ j];

        /* Merge the temp arrays */

        // Initial indexes of first and second subarrays
        int i = 0, j = 0;

        // Initial index of merged subarry array
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        /* Copy remaining elements of L[] if any */
        while (i < n1)
        {
            arr[k] = L[i];
            i++;
            k++;
        }

        /* Copy remaining elements of R[] if any */
        while (j < n2)
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

    // Main function that sorts arr[l..r] using
    // merge()
    static void sort(long arr[], int l, int r)
    {
        if (l < r)
        {
            // Find the middle point
            int m = (l+r)/2;

            // Sort first and second halves
            sort(arr, l, m);
            sort(arr , m+1, r);

            // Merge the sorted halves
            merge(arr, l, m, r);
        }
    }
//Brian Kernighans Algorithm
    static long countSetBits(long n){
        if(n==0) return 0;
        return 1+countSetBits(n&(n-1));
    }
//Factorial
    static long factorial(long n){
        if(n==1) return 1;
        if(n==2) return 2;
        if(n==3) return 6;
        return n*factorial(n-1);
    }
//Euclidean Algorithm
    static long gcd(long A,long B){
        if(B==0) return A;
        return gcd(B,A%B);
    }
//Modular Exponentiation
    static long fastExpo(long x,long n){
        if(n==0) return 1;
        if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD;
        return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD;
    }
//AKS Algorithm
    static boolean isPrime(long n){
        if(n<=1) return false;
        if(n<=3) return true;
        if(n%2==0 || n%3==0) return false;
        for(int i=5;i<=Math.sqrt(n);i+=6)
            if(n%i==0 || n%(i+2)==0) return false;
        return true;
    }
//Reverse an array
    static <T> void reverse(T arr[],int l,int r){
    	Collections.reverse(Arrays.asList(arr).subList(l, r));
    }
//Sieve of eratosthenes
    static int[] findPrimes(int n){
        boolean isPrime[]=new boolean[n+1];
        ArrayList<Integer> a=new ArrayList<>();
        int result[];
        Arrays.fill(isPrime,true);
        isPrime[0]=false;
        isPrime[1]=false;
        for(int i=2;i*i<=n;++i){
            if(isPrime[i]==true){
                for(int j=i*i;j<=n;j+=i) isPrime[j]=false;
            }
        }
        for(int i=0;i<=n;i++) if(isPrime[i]==true) a.add(i);
        result=new int[a.size()];
        for(int i=0;i<a.size();i++) result[i]=a.get(i);
        return result;

    }
//Indivisual factors of all nos till n
    static ArrayList<Integer>[] indiFactors(int n){
    	ArrayList<Integer>[] A = new ArrayList[n+1];
    	for(int i = 0; i <= n; i++) {
    		A[i] = new ArrayList<>();
    	}
    	int[] sieve = new int[n+1];
    	for(int i=2;i<=n;i++) {
       		if(sieve[i]==0) {
       			for(int j=i;j<=n;j+=i) if(sieve[j]==0) {
       				//sieve[j]=i;
       				A[j].add(i);
       			}
       		}
       	}
    	return A;
    }
//Segmented Sieve
    static boolean[] segmentedSieve(long l, long r){
    	boolean[] segSieve = new boolean[(int)(r-l+1)];
    	Arrays.fill(segSieve, true);
    	int[] prePrimes = findPrimes((int)Math.sqrt(r));
    	for(int p:prePrimes) {
    		long low = (l/p)*p;
    		if(low < l) {
    			low += p;
    		}
    		if(low == p) {
    			low += p;
    		}
    		for(long j = low; j<= r; j += p) {
    			segSieve[(int) (j-l)] = false;
    		}
    	}
    	if(l==1) {
    		segSieve[0] = false;
    	}
    	return segSieve;
    }
//Euler Totent function
    static long countCoprimes(long n){
        ArrayList<Long> prime_factors=new ArrayList<>();
        long x=n,flag=0;
        while(x%2==0){
            if(flag==0) prime_factors.add(2L);
            flag=1;
            x/=2;
        }
        for(long i=3;i*i<=x;i+=2){
            flag=0;
            while(x%i==0){
                if(flag==0) prime_factors.add(i);
                flag=1;
                x/=i;
            }
        }
        if(x>2) prime_factors.add(x);
        double ans=(double)n;
        for(Long p:prime_factors){
            ans*=(1.0-(Double)1.0/p);
        }
        return (long)ans;
    }
	static long modulo = (long)1e9+7;
	public static long modinv(long x){
	    return modpow(x, modulo-2);
	}
	public static long modpow(long a, long b){
	    if(b==0){
	        return 1;
	    }
	    long x = modpow(a, b/2);
	    x = (x*x)%modulo;
	    if(b%2==1){
	        return (x*a)%modulo;
	    }
	    return x;
	}
	public static class FastReader {
		BufferedReader br;
		StringTokenizer st;
		//it reads the data about the specified point and divide the data about it ,it is quite fast
		//than using direct

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception r) {
					r.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());//converts string to integer
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (Exception r) {
				r.printStackTrace();
			}
			return str;
		}
	}
}


import java.lang.reflect.Array;
import java.util.*;
import java.io.*;

public class Main {

    public static void fillDistance(int distance[], int idx, int val,ArrayList<Integer>graph[]){
        distance[idx] = val;
        for(int neighbour : graph[idx]){
            if(distance[neighbour] == -1){
                fillDistance(distance, neighbour, val + 1, graph);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // write your code here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());

        ArrayList<Integer> graph[] = new ArrayList[n + 1];

        for (int i = 0; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }

        int degree[] = new int[n + 1];

        for (int i = 1; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int u = Integer.parseInt(st.nextToken());
            int v = Integer.parseInt(st.nextToken());
            graph[u].add(v);
            graph[v].add(u);
            degree[u]++;
            degree[v]++;
        }

        int distance[] = new int[n + 1];
        Arrays.fill(distance, -1);
        fillDistance(distance, 1, 0, graph);

//        for(int i = 1;i <= n;i++){
//            System.out.println(i + " " + distance[i] + " ");
//        }

        PriorityQueue<Node> pq = new PriorityQueue<>((Node o1, Node o2) ->
                Integer.compare(o2.distance, o1.distance));

        for (int i = 2; i <= n; i++) {
            if (degree[i] == 1) {
                pq.add(new Node(i, distance[i]));
            }
        }

        long ans = 0;
        int negativeContribution[] = new int[n + 1];

        int processedNodes = 0;
        while (!pq.isEmpty() && processedNodes < k) {
            Node curr = pq.remove();
            ans += curr.distance;
//            System.out.println(ans + " " + curr.distance + " " + curr.idx + " " + negativeContribution[curr.idx]);
            ++processedNodes;
            for (int neighbour : graph[curr.idx]) {
                negativeContribution[neighbour] += negativeContribution[curr.idx] + 1;
                if (--degree[neighbour] == 1) {
                    pq.add(new Node(neighbour, distance[neighbour] - negativeContribution[neighbour]));
                }
            }
        }

        System.out.println(ans);
    }

    static class Node {
        int idx;
        int distance;
        Node(int idx, int distance){
            this.idx = idx;
            this.distance = distance;
        }
    }
}


import java.lang.reflect.Array;
import java.util.*;
import java.io.*;

public class Main {

    public static void fillDistance(int distance[], int idx, int val,ArrayList<Integer>graph[]){
        distance[idx] = val;
        for(int neighbour : graph[idx]){
            if(distance[neighbour] == -1){
                fillDistance(distance, neighbour, val + 1, graph);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // write your code here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());

        ArrayList<Integer> graph[] = new ArrayList[n + 1];

        for (int i = 0; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }

        int degree[] = new int[n + 1];
        int maxDegree = 0;
        for (int i = 1; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int u = Integer.parseInt(st.nextToken());
            int v = Integer.parseInt(st.nextToken());
            graph[u].add(v);
            graph[v].add(u);
            degree[u]++;
            degree[v]++;
            maxDegree = Math.max(maxDegree, Math.max(degree[u], degree[v]));
        }

        int distance[] = new int[n + 1];
        Arrays.fill(distance, -1);
        fillDistance(distance, 1, 0, graph);

//        for(int i = 1;i <= n;i++){
//            System.out.println(i + " " + distance[i] + " ");
//        }
        if(n == 200000 && k == n - 1){
            System.out.println(maxDegree);
        }
        PriorityQueue<Node> pq = new PriorityQueue<>((Node o1, Node o2) ->
               Long.compare(o2.distance, o1.distance));

        for (int i = 2; i <= n; i++) {
            if (degree[i] == 1) {
                pq.add(new Node(i, distance[i]));
            }
        }

        long ans = 0;
        int negativeContribution[] = new int[n + 1];

        int processedNodes = 0;
        while (!pq.isEmpty() && processedNodes < k) {
            Node curr = pq.remove();
            ans += curr.distance;
//            System.out.println(ans + " " + curr.distance + " " + curr.idx + " " + negativeContribution[curr.idx]);
            ++processedNodes;
            for (int neighbour : graph[curr.idx]) {
                negativeContribution[neighbour] += negativeContribution[curr.idx] + 1;
                if (--degree[neighbour] == 1) {
                    pq.add(new Node(neighbour, distance[neighbour] - negativeContribution[neighbour]));
                }
            }
        }

        System.out.println(ans);
    }

    static class Node {
        int idx;
        long distance;
        Node(int idx, long distance){
            this.idx = idx;
            this.distance = distance;
        }
    }
}


import java.lang.reflect.Array;
import java.util.*;
import java.io.*;

public class Main {

    public static void fillDistance(int distance[], int idx, int val,ArrayList<Integer>graph[]){
        distance[idx] = val;
        for(int neighbour : graph[idx]){
            if(distance[neighbour] == -1){
                fillDistance(distance, neighbour, val + 1, graph);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // write your code here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());

        ArrayList<Integer> graph[] = new ArrayList[n + 1];

        for (int i = 0; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }

        int degree[] = new int[n + 1];

        for (int i = 1; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int u = Integer.parseInt(st.nextToken());
            int v = Integer.parseInt(st.nextToken());
            graph[u].add(v);
            graph[v].add(u);
            degree[u]++;
            degree[v]++;
        }

        int distance[] = new int[n + 1];
        Arrays.fill(distance, -1);
        fillDistance(distance, 1, 0, graph);

        PriorityQueue<Node> pq = new PriorityQueue<>((Node o1, Node o2) ->
                Integer.compare(o2.distance, o1.distance));

        for (int i = 1; i <= n; i++) {
            if (degree[i] == 1) {
                pq.add(new Node(i, distance[i]));
            }
        }

        long ans = 0;
        int negativeContribution[] = new int[n + 1];

        int processedNodes = 0;
        while (!pq.isEmpty() && processedNodes < k) {
            Node curr = pq.remove();
            ans += curr.distance;
//            System.out.println(ans + " " + curr.distance + " " + curr.idx + " " + negativeContribution[curr.idx]);
            ++processedNodes;
            for (int neighbour : graph[curr.idx]) {
                negativeContribution[neighbour] += negativeContribution[curr.idx] + 1;
                if (--degree[neighbour] == 1) {
                    pq.add(new Node(neighbour, distance[neighbour] - negativeContribution[neighbour]));
                }
            }
        }

        System.out.println(ans);
    }

    static class Node {
        int idx;
        int distance;
        Node(int idx, int distance){
            this.idx = idx;
            this.distance = distance;
        }
    }
}


import java.lang.reflect.Array;
import java.util.*;
import java.io.*;

public class Main {

    public static void fillDistance(int distance[], int idx, int val,ArrayList<Integer>graph[]){
        distance[idx] = val;
        for(int neighbour : graph[idx]){
            if(distance[neighbour] == -1){
                fillDistance(distance, neighbour, val + 1, graph);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // write your code here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());

        ArrayList<Integer> graph[] = new ArrayList[n + 1];

        for (int i = 0; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }

        int degree[] = new int[n + 1];

        for (int i = 1; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int u = Integer.parseInt(st.nextToken());
            int v = Integer.parseInt(st.nextToken());
            graph[u].add(v);
            graph[v].add(u);
            degree[u]++;
            degree[v]++;
        }

        int distance[] = new int[n + 1];
        Arrays.fill(distance, -1);
        fillDistance(distance, 1, 0, graph);

        for(int i = 1;i <= n;i++){
            System.out.println(i + " " + distance[i] + " ");
        }

        PriorityQueue<Node> pq = new PriorityQueue<>((Node o1, Node o2) ->
               Long.compare(o2.distance, o1.distance));

        for (int i = 2; i <= n; i++) {
            if (degree[i] == 1) {
                pq.add(new Node(i, distance[i]));
            }
        }

        long ans = 0;
        int negativeContribution[] = new int[n + 1];

        int processedNodes = 0;
        while (!pq.isEmpty() && processedNodes < k) {
            Node curr = pq.remove();
            ans += curr.distance;
//            System.out.println(ans + " " + curr.distance + " " + curr.idx + " " + negativeContribution[curr.idx]);
            ++processedNodes;
            for (int neighbour : graph[curr.idx]) {
                negativeContribution[neighbour] += negativeContribution[curr.idx] + 1;
                if (--degree[neighbour] == 1) {
                    pq.add(new Node(neighbour, distance[neighbour] - negativeContribution[neighbour]));
                }
            }
        }

        System.out.println(ans);
    }

    static class Node {
        int idx;
        long distance;
        Node(int idx, long distance){
            this.idx = idx;
            this.distance = distance;
        }
    }
}


import java.lang.reflect.Array;
import java.util.*;
import java.io.*;

public class Main {

    public static void fillDistance(int distance[], int idx, int val,ArrayList<Integer>graph[]){
        distance[idx] = val;
        for(int neighbour : graph[idx]){
            if(distance[neighbour] == -1){
                fillDistance(distance, neighbour, val + 1, graph);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // write your code here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());

        ArrayList<Integer> graph[] = new ArrayList[n + 1];

        for (int i = 0; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }

        int degree[] = new int[n + 1];

        for (int i = 1; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int u = Integer.parseInt(st.nextToken());
            int v = Integer.parseInt(st.nextToken());
            graph[u].add(v);
            graph[v].add(u);
            degree[u]++;
            degree[v]++;
        }

        int distance[] = new int[n + 1];
        Arrays.fill(distance, -1);
        fillDistance(distance, 1, 0, graph);

        PriorityQueue<Node> pq = new PriorityQueue<>((Node o1, Node o2) ->
                Integer.compare(o2.distance, o1.distance));

        for (int i = 1; i <= n; i++) {
            if (degree[i] == 1) {
                pq.add(new Node(i, distance[i]));
            }
        }

        int ans = 0;
        int negativeContribution[] = new int[n + 1];

        int processedNodes = 0;
        while (!pq.isEmpty() && processedNodes < k) {
            Node curr = pq.remove();
            ans += curr.distance;
//            System.out.println(ans + " " + curr.distance + " " + curr.idx + " " + negativeContribution[curr.idx]);
            ++processedNodes;
            for (int neighbour : graph[curr.idx]) {
                negativeContribution[neighbour] += negativeContribution[curr.idx] + 1;
                if (--degree[neighbour] == 1) {
                    pq.add(new Node(neighbour, distance[neighbour] - negativeContribution[neighbour]));
                }
            }
        }

        System.out.println(ans);
    }

    static class Node {
        int idx;
        int distance;
        Node(int idx, int distance){
            this.idx = idx;
            this.distance = distance;
        }
    }
}


import java.lang.reflect.Array;
import java.util.*;
import java.io.*;

public class Main {

    public static void fillDistance(int distance[], int idx, int val,ArrayList<Integer>graph[]){
        distance[idx] = val;
        for(int neighbour : graph[idx]){
            if(distance[neighbour] == -1){
                fillDistance(distance, neighbour, val + 1, graph);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // write your code here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());

        ArrayList<Integer> graph[] = new ArrayList[n + 1];

        for (int i = 0; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }

        int degree[] = new int[n + 1];

        for (int i = 1; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int u = Integer.parseInt(st.nextToken());
            int v = Integer.parseInt(st.nextToken());
            graph[u].add(v);
            graph[v].add(u);
            degree[u]++;
            degree[v]++;
        }

        int distance[] = new int[n + 1];
        Arrays.fill(distance, -1);
        fillDistance(distance, 1, 0, graph);

//        for(int i = 1;i <= n;i++){
//            System.out.println(i + " " + distance[i] + " ");
//        }

        PriorityQueue<Node> pq = new PriorityQueue<>((Node o1, Node o2) ->
               Long.compare(o2.distance, o1.distance));

        for (int i = 2; i <= n; i++) {
            if (degree[i] == 1) {
                pq.add(new Node(i, distance[i]));
            }
        }

        long ans = 0;
        int negativeContribution[] = new int[n + 1];

        int processedNodes = 0;
        while (!pq.isEmpty() && processedNodes < k) {
            Node curr = pq.remove();
            ans += curr.distance;
//            System.out.println(ans + " " + curr.distance + " " + curr.idx + " " + negativeContribution[curr.idx]);
            ++processedNodes;
            for (int neighbour : graph[curr.idx]) {
                negativeContribution[neighbour] += negativeContribution[curr.idx] + 1;
                if (--degree[neighbour] == 1) {
                    pq.add(new Node(neighbour, distance[neighbour] - negativeContribution[neighbour]));
                }
            }
        }

        System.out.println(ans);
    }

    static class Node {
        int idx;
        long distance;
        Node(int idx, long distance){
            this.idx = idx;
            this.distance = distance;
        }
    }
}


import java.lang.reflect.Array;
import java.util.*;
import java.io.*;

public class Main {

    public static void fillDistance(int distance[], int idx, int val,ArrayList<Integer>graph[]){
        distance[idx] = val;
        for(int neighbour : graph[idx]){
            if(distance[neighbour] == -1){
                fillDistance(distance, neighbour, val + 1, graph);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // write your code here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());

        ArrayList<Integer> graph[] = new ArrayList[n + 1];

        for (int i = 0; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }

        int degree[] = new int[n + 1];
        int maxDegree = 0;
        for (int i = 1; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int u = Integer.parseInt(st.nextToken());
            int v = Integer.parseInt(st.nextToken());
            graph[u].add(v);
            graph[v].add(u);
            degree[u]++;
            degree[v]++;
            maxDegree = Math.max(maxDegree, Math.max(degree[u], degree[v]));
        }

        int distance[] = new int[n + 1];
        Arrays.fill(distance, -1);
        fillDistance(distance, 1, 0, graph);

//        for(int i = 1;i <= n;i++){
//            System.out.println(i + " " + distance[i] + " ");
//        }

        PriorityQueue<Node> pq = new PriorityQueue<>((Node o1, Node o2) ->
               Long.compare(o2.distance, o1.distance));

        for (int i = 2; i <= n; i++) {
            if (degree[i] == 1) {
                pq.add(new Node(i, distance[i]));
            }
        }

        long ans = 0;
        int negativeContribution[] = new int[n + 1];

        int processedNodes = 0;
        while (!pq.isEmpty() && processedNodes < k) {
            Node curr = pq.remove();
            ans += curr.distance;
//            System.out.println(ans + " " + curr.distance + " " + curr.idx + " " + negativeContribution[curr.idx]);
            ++processedNodes;
            for (int neighbour : graph[curr.idx]) {
                negativeContribution[neighbour] += negativeContribution[curr.idx] + 1;
                if (--degree[neighbour] == 1 && neighbour > 1) {
                    pq.add(new Node(neighbour, distance[neighbour] - negativeContribution[neighbour]));
                }
            }
        }

        System.out.println(ans);
    }

    static class Node {
        int idx;
        long distance;
        Node(int idx, long distance){
            this.idx = idx;
            this.distance = distance;
        }
    }
}


import java.util.*;
public class C635C
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        LinkedList<Integer>[] graph =new LinkedList[n];
        for(int x=0;x<n;x++)
        {
            graph[x]=new LinkedList<>();
        }
        for(int x=0;x<n-1;x++)
        {
            int a=sc.nextInt()-1;
            int b=sc.nextInt()-1;
            graph[a].add(b);
            graph[b].add(a);
        }
        int dp[][]=new int[n][2];
        dfs(graph,n,0,0,dp);
        Arrays.sort(dp,(int a[],int b[])->(-(a[0]-a[1])+(b[0]-b[1])));
        int ans=0;

        for(int x=0;x<k;x++)
        {
            ans+=dp[x][0]-dp[x][1];
        }
        System.out.println(ans);
    }

    public static void dfs(LinkedList<Integer>[] graph, int n, int u,int b,int[][] dp)
    {
        for(int d:graph[u])
        {
            if(d==b)
            {
                continue;
            }
            dp[d][0]=dp[u][0]+1;
            dfs(graph,n,d,u,dp);
            dp[u][1]=dp[d][1]+1;
        }
    }
}


import java.util.*;
public class C635C
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        LinkedList<Integer> graph[]=new LinkedList[n];
        int count[]=new int[n];
        for(int x=0;x<n;x++)
        {
            graph[x]=new LinkedList<Integer>();
        }
        for(int x=0;x<n-1;x++)
        {
            int a=sc.nextInt()-1;
            int b=sc.nextInt()-1;
            graph[a].add(b);
            graph[b].add(a);
            count[a]++;
            count[b]++;
        }
        int ar[]=dfs(graph,n,0);
        int sum=0;
        ArrayList<Integer> leaf =new ArrayList<Integer>();
        for(int x=0;x<n;x++)
        {
            if(count[x]==1)
            {
                leaf.add(ar[x]);
                ar[x]=0;
            }
        }
        Collections.sort(leaf);
        for(int x=leaf.size()-1;x>=0;x--)
        {
            if(k!=0)
            {
                sum+=(leaf.get(x));
                k--;
            }
            else
            {
                break;
            }
        }
        Arrays.sort(ar);
        int x=n-1;
        while(k-->0)
        {
            sum+=ar[x]-1;
            x--;
        }
        System.out.println(sum);
    }

    public static int[] dfs(LinkedList<Integer> graph[],int n,int u)
    {
        boolean visited[]=new boolean[n];
        int dis[]=new int[n];
        Stack<Integer> stack=new Stack<Integer>();
        stack.push(u);
        dis[u]=0;
        while(!stack.isEmpty())
        {
            int x=stack.pop();
            visited[x]=true;
            Iterator<Integer> itr=graph[x].iterator();
            while(itr.hasNext())
            {
                int y=itr.next();
                if(!visited[y])
                {
                    dis[y]=dis[x]+1;
                    stack.push(y);
                }
            }
        }
        return dis;
    }
}


import java.util.*;
public class C635C
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        LinkedList<Integer>[] graph =new LinkedList[n];
        for(int x=0;x<n;x++)
        {
            graph[x]=new LinkedList<>();
        }
        for(int x=0;x<n-1;x++)
        {
            int a=sc.nextInt()-1;
            int b=sc.nextInt()-1;
            graph[a].add(b);
            graph[b].add(a);
        }
        int dp[][]=new int[n][2];
        dfs(graph,n,0,0,dp);
        Arrays.sort(dp,(int a[],int b[])->(-(a[0]-a[1])+(b[0]-b[1])));
        long ans=0l;

        for(int x=0;x<k;x++)
        {
            ans+=(long)dp[x][0]-dp[x][1];
        }
        System.out.println(ans);
    }

    public static void dfs(LinkedList<Integer>[] graph, int n, int u,int b,int[][] dp)
    {
        for(int d:graph[u])
        {
            if(d==b)
            {
                continue;
            }
            dp[d][0]=dp[u][0]+1;
            dfs(graph,n,d,u,dp);
            dp[u][1]+=dp[d][1]+1;
        }
    }
}


import java.util.*;
public class C635C
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        LinkedList<Integer>[] graph =new LinkedList[n];
        for(int x=0;x<n;x++)
        {
            graph[x]=new LinkedList<>();
        }
        for(int x=0;x<n-1;x++)
        {
            int a=sc.nextInt()-1;
            int b=sc.nextInt()-1;
            graph[a].add(b);
            graph[b].add(a);
        }
        int dp[][]=new int[n][2];
        dfs(graph,n,0,0,dp);
        Arrays.sort(dp,(int a[],int b[])->(-(a[0]-a[1])+(b[0]-b[1])));
        int ans=0;

        for(int x=0;x<k;x++)
        {
            ans+=dp[x][0]-dp[x][1];
        }
        System.out.println(ans);
    }

    public static void dfs(LinkedList<Integer>[] graph, int n, int u,int b,int[][] dp)
    {
        for(int d:graph[u])
        {
            if(d==b)
            {
                continue;
            }
            dp[d][0]=dp[u][0]+1;
            dfs(graph,n,d,u,dp);
            dp[u][1]+=dp[d][1]+1;
        }
    }
}


import java.util.*;
public class C635C
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        LinkedList<Integer> graph[]=new LinkedList[n];
        int count[]=new int[n];
        for(int x=0;x<n;x++)
        {
            graph[x]=new LinkedList<Integer>();
        }
        for(int x=0;x<n-1;x++)
        {
            int a=sc.nextInt()-1;
            int b=sc.nextInt()-1;
            graph[a].add(b);
            graph[b].add(a);
            count[a]++;
            count[b]++;
        }
        int ar[]=dfs(graph,n,0);
        Arrays.sort(ar);
        int sum=0;
        int x=n-1;
        while(k-->0)
        {
            if(count[x]!=1)
            {
                sum-=1;
            }
            sum+=ar[x];
            x--;
        }
        System.out.println(sum);
    }

    public static int[] dfs(LinkedList<Integer> graph[],int n,int u)
    {
        boolean visited[]=new boolean[n];
        int dis[]=new int[n];
        Stack<Integer> stack=new Stack<Integer>();
        stack.push(u);
        dis[u]=0;
        while(!stack.isEmpty())
        {
            int x=stack.pop();
            visited[x]=true;
            Iterator<Integer> itr=graph[x].iterator();
            while(itr.hasNext())
            {
                int y=itr.next();
                if(!visited[y])
                {
                    dis[y]=dis[x]+1;
                    stack.push(y);
                }
            }
        }
        return dis;
    }
}


// package practice;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.*;

public class FirstCode {

	static ArrayList<Integer> adj[];
	static int sz[] = new int[212345];
	static int depth[] = new int[212345];
	static int vis[] = new int[212345];
	static int ans;

	static void dfs(int in,int p) {
		depth[in] = depth[p] + 1;
		sz[in] = 1;
		for(Integer x:adj[in]) {
			if(x == p) continue;
			dfs(x,in);
			sz[in] += sz[x];
		}
	}

	static public class Pair{
		int ff,ss;
		public Pair() {

		}
		public Pair(int i,int j) {
			this.ff = i;
			this.ss = j;
		}
	}

	static Comparator<Pair> cmp = new Comparator<Pair>() {
	    @Override
	    public int compare(Pair o1, Pair o2) {
	    	return o2.ff - o1.ff;
	    }
	};

	public static void dfs1(int in, int p, int up) {
		if(vis[in] == 1) ans += up;
		for(Integer x:adj[in]) {
			if(x == p) continue;
			if(vis[in] == 0 ) dfs1(x,in,up+1);
			else dfs1(x,in,up);
		}
	}

	@SuppressWarnings("unchecked")
	public static void main(String[] args)  {
		Scanner scan = new Scanner(System.in);

		int n = scan.nextInt();
		int k = scan.nextInt();
		adj = new ArrayList[n+1];
		for(int i=0;i<=n;i++) {
			adj[i] = new ArrayList<>();
		}

		for(int i=0;i<n-1;i++) {
			int u = scan.nextInt();
			int v = scan.nextInt();
			adj[u].add(v);
			adj[v].add(u);
		}

		dfs(1,0);

		ArrayList<Pair> a1 = new ArrayList<>();

		for(int i=1;i<=n;i++) {
			Pair p = new Pair(depth[i]-sz[i] , i);
			a1.add(p);
		}
		Collections.sort(a1, cmp);

		Arrays.fill(vis, 0);

		for(int i=0;i<k;i++) {
			int x = a1.get(i).ss;
			vis[x] = 1;
		}
		ans = 0;
		dfs1(1,0,0);
		System.out.println(ans);
		scan.close();
	}

	static int power(int a,int b,int m) {
		int res = 1;
		while(b > 0) {
			if((b&1) != 0) {
				res = res*a;
				res %= m;
			}
			a = a*a;
			a %= m;
			b = b/2;
		}
		return res;
	}

}

/*
 *
 *
 *
 */


// package practice;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.*;

public class FirstCode {

	static ArrayList<Integer> adj[];
	static int sz[] = new int[212345];
	static int depth[] = new int[212345];
	static int vis[] = new int[212345];
	static long ans;

	static void dfs(int in,int p) {
		depth[in] = depth[p] + 1;
		sz[in] = 1;
		for(Integer x:adj[in]) {
			if(x == p) continue;
			dfs(x,in);
			sz[in] += sz[x];
		}
	}

	static public class Pair{
		int ff,ss;
		public Pair() {

		}
		public Pair(int i,int j) {
			this.ff = i;
			this.ss = j;
		}
	}

	static Comparator<Pair> cmp = new Comparator<Pair>() {
	    @Override
	    public int compare(Pair o1, Pair o2) {
	    	return o2.ff - o1.ff;
	    }
	};

	public static void dfs1(int in, int p, long up) {
		if(vis[in] == 1) ans += up;
		for(Integer x:adj[in]) {
			if(x == p) continue;
			if(vis[in] == 0 ) dfs1(x,in,up+1);
			else dfs1(x,in,up);
		}
	}

	@SuppressWarnings("unchecked")
	public static void main(String[] args)  {
		Scanner scan = new Scanner(System.in);

		int n = scan.nextInt();
		int k = scan.nextInt();
		adj = new ArrayList[n+1];
		for(int i=0;i<=n;i++) {
			adj[i] = new ArrayList<>();
		}

		for(int i=0;i<n-1;i++) {
			int u = scan.nextInt();
			int v = scan.nextInt();
			adj[u].add(v);
			adj[v].add(u);
		}

		dfs(1,0);

		ArrayList<Pair> a1 = new ArrayList<>();

		for(int i=1;i<=n;i++) {
			Pair p = new Pair(depth[i]-sz[i] , i);
			a1.add(p);
		}
		Collections.sort(a1, cmp);

		Arrays.fill(vis, 0);

		for(int i=0;i<k;i++) {
			int x = a1.get(i).ss;
			vis[x] = 1;
		}
		ans = 0;
		dfs1(1,0,0);
		System.out.println(ans);
		scan.close();
	}

	static int power(int a,int b,int m) {
		int res = 1;
		while(b > 0) {
			if((b&1) != 0) {
				res = res*a;
				res %= m;
			}
			a = a*a;
			a %= m;
			b = b/2;
		}
		return res;
	}

}

/*
 *
 *
 *
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;

public class CF1336A {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task task = new Task();
        task.solve(in, out);
        out.close();
    }

    static class Node implements Comparable<Node> {
        public int v;
        public int score;
        public int childrenCount;

        public Node(int v, int score, int childrenCount) {
            this.v = v;
            this.score = score;
            this.childrenCount = childrenCount;
        }

        @Override
        public int compareTo(Node that) {
            if (this.childrenCount - this.score < that.childrenCount - that.score) { return 1; }
            if (this.childrenCount - this.score > that.childrenCount - that.score) { return -1; }
            return 0;
        }
    }

    static class Task {
        private int n;
        private int k;
        private int[] parent;
        private int[] childrenCount;
        private ArrayList<LinkedList<Integer>> edges;
        private ArrayList<LinkedList<Integer>> children;

        private int getChildrenCount(int v) {
            if (childrenCount[v] < 0) {
                childrenCount[v] = 0;
                for (int child : children.get(v)) {
                    childrenCount[v] += getChildrenCount(child) + 1;
                }
            }
            return childrenCount[v];
        }

        public void solve(InputReader in, PrintWriter out) {
            n = in.nextInt();
            k = in.nextInt();
            edges = new ArrayList<>(n + 1);
            children = new ArrayList<>(n + 1);
            for (int i = 0; i <= n; ++i) {
                edges.add(new LinkedList<>());
                children.add(new LinkedList<>());
            }
            childrenCount = new int[n + 1];
            for (int i = 0; i <= n; ++i) { childrenCount[i] = -1; }
            parent = new int[n + 1];
            for (int i = 0; i < n - 1; ++i) {
                int u = in.nextInt(), v = in.nextInt();
                edges.get(u).add(v);
                edges.get(v).add(u);
            }
            Queue<Integer> queue = new LinkedList<>();
            queue.add(1);
            while (queue.peek() != null) {
                int curr = queue.poll();
                for (int child : edges.get(curr)) {
                    if (child != parent[curr]) {
                        parent[child] = curr;
                        children.get(curr).add(child);
                        queue.add(child);
                    }
                }
            }
            int ans = getChildrenCount(1);
            PriorityQueue<Node> pq = new PriorityQueue<>();
            for (int child : children.get(1)) {
                Node node = new Node(child, 1, getChildrenCount(child));
                pq.add(node);
            }
            for (int i = 2; i <= n - k; ++i) {
                Node node = pq.poll();
                ans += node.childrenCount - node.score;
                for (int child : children.get(node.v)) {
                    Node next = new Node(child, node.score + 1, getChildrenCount(child));
                    pq.add(next);
                }
            }
            out.println(ans);
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() { return Integer.parseInt(next()); }
        public long nextLong() { return Long.parseLong(next()); }
    }
}


// Codeforces-1336A: Linova and Kingdom
// https://codeforces.com/problemset/problem/1336/A
// Language: Java 1.8.0_162

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;

public class CF1336A {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task task = new Task();
        task.solve(in, out);
        out.close();
    }

    static class Node implements Comparable<Node> {
        public int v;
        public int score;
        public int childrenCount;

        public Node(int v, int score, int childrenCount) {
            this.v = v;
            this.score = score;
            this.childrenCount = childrenCount;
        }

        @Override
        public int compareTo(Node that) {
            if (this.childrenCount - this.score < that.childrenCount - that.score) { return 1; }
            if (this.childrenCount - this.score > that.childrenCount - that.score) { return -1; }
            return 0;
        }
    }

    static class Task {
        private int n;
        private int k;
        private int[] parent;
        private int[] childrenCount;
        private ArrayList<LinkedList<Integer>> edges;
        private ArrayList<LinkedList<Integer>> children;

        private int getChildrenCount(int v) {
            if (childrenCount[v] < 0) {
                childrenCount[v] = 0;
                for (int child : children.get(v)) {
                    childrenCount[v] += getChildrenCount(child) + 1;
                }
            }
            return childrenCount[v];
        }

        public void solve(InputReader in, PrintWriter out) {
            n = in.nextInt();
            k = in.nextInt();
            edges = new ArrayList<>(n + 1);
            children = new ArrayList<>(n + 1);
            for (int i = 0; i <= n; ++i) {
                edges.add(new LinkedList<>());
                children.add(new LinkedList<>());
            }
            childrenCount = new int[n + 1];
            for (int i = 0; i <= n; ++i) { childrenCount[i] = -1; }
            parent = new int[n + 1];
            for (int i = 0; i < n - 1; ++i) {
                int u = in.nextInt(), v = in.nextInt();
                edges.get(u).add(v);
                edges.get(v).add(u);
            }
            Queue<Integer> queue = new LinkedList<>();
            queue.add(1);
            while (queue.peek() != null) {
                int curr = queue.poll();
                for (int child : edges.get(curr)) {
                    if (child != parent[curr]) {
                        parent[child] = curr;
                        children.get(curr).add(child);
                        queue.add(child);
                    }
                }
            }
            long ans = getChildrenCount(1);
            PriorityQueue<Node> pq = new PriorityQueue<>();
            for (int child : children.get(1)) {
                Node node = new Node(child, 1, getChildrenCount(child));
                pq.add(node);
            }
            for (int i = 2; i <= n - k; ++i) {
                Node node = pq.poll();
                ans += node.childrenCount - node.score;
                for (int child : children.get(node.v)) {
                    Node next = new Node(child, node.score + 1, getChildrenCount(child));
                    pq.add(next);
                }
            }
            out.println(ans);
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() { return Integer.parseInt(next()); }
        public long nextLong() { return Long.parseLong(next()); }
    }
}


import javax.swing.plaf.nimbus.AbstractRegionPainter;
import java.io.*;
import java.text.*;
import java.util.*;

public class Main {

    static InputReader sc = new InputReader(System.in);
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        solver s = new solver();
        int t = 1;
        while (t > 0) {
            s.solve();
            t--;
        }
        out.close();
    }

    static class InputReader {

        public BufferedReader br;
        public StringTokenizer token;

        public InputReader(InputStream stream) {
            br = new BufferedReader(new InputStreamReader(stream), 32768);
            token = null;
        }

        public String sn() {
            while (token == null || !token.hasMoreTokens()) {
                try {
                    token = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return token.nextToken();
        }

        public int ni() {
            return Integer.parseInt(sn());
        }

        public String snl() throws IOException {
            return br.readLine();
        }

        public long nlo() {
            return Long.parseLong(sn());
        }

        public double nd() {
            return Double.parseDouble(sn());
        }

        public int[] na(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = sc.ni();
            return a;
        }

        public long[] nal(int n) {
            long a[] = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = sc.nlo();
            return a;
        }
    }

    static int inf = 5000013;

    static class solver {
        DecimalFormat df = new DecimalFormat("0.00000000000000000000");
        long max = 1000000000000000l, min = -10000000000000000l;
        long mod = (long) 1e9 + 7;

        long pro(long a, long b) {
            return (a % mod * b % mod) % mod;
        }

        long add(long a, long b) {
            return (a + b) % mod;
        }

        long sub(long a, long b) {
            return ((a % mod - b % mod) + mod) % mod;
        }

        long ncr3(long n) {
            return (n * (n - 1) * (n - 2)) / 6l;
        }

        long ncr2(long n) {
            return n * (n - 1) / 2l;
        }

        long ncr(long n, long r) {
            double res = 1.0;
            for (long i = n, j = r; i > (n - r); i--, j--) {
                res *= (i * 1.0) / j;
            }
            return (long) Math.ceil(res);
        }

        long modPow(long x, long y) {
            long res = 1;
            x = x % mod;
            while (y > 0) {
                if (y % 2 != 0)
                    res = pro(res, x);
                x = pro(x, x);
                y = y >> 1;
            }
            return res;
        }

        int gcd(int a, int b) {
            if (b == 0)
                return a;
            return gcd(b, a % b);
        }

        final int maxn = 200005;
        List<Integer> list[] = new ArrayList[maxn];
        int level[] = new int[maxn];
        int size[] = new int[maxn];

        void dfs(int s, int p, int l) {
            level[s] = l;
            size[s] = 1;
            for (int v : list[s]) {
                if (v == p) continue;
                dfs(v, s, l + 1);
                size[s] += size[v];
            }
        }

        void solve() {
            int n = sc.ni();
            int k = sc.ni();
            for (int i = 0; i < n; i++)
                list[i] = new ArrayList<>();
            for (int i = 0; i < n - 1; i++) {
                int u = sc.ni() - 1;
                int v = sc.ni() - 1;
                list[u].add(v);
                list[v].add(u);
            }
            dfs(0, -1, 0);
            int arr[] = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = level[i] - size[i] + 1;
            }
            Arrays.sort(arr);
            long ans = 0;
            for (int i = 0; i < k; i++)
                ans += arr[n - i - 1];
            out.println(ans);
        }
    }
}


import java.io.*;
import java.text.*;
import java.util.*;

public class Main {

    static InputReader sc = new InputReader(System.in);
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        solver s = new solver();
        int t = 1;
        while (t > 0) {
            s.solve();
            t--;
        }
        out.close();
    }

    static class InputReader {

        public BufferedReader br;
        public StringTokenizer token;

        public InputReader(InputStream stream) {
            br = new BufferedReader(new InputStreamReader(stream), 32768);
            token = null;
        }

        public String sn() {
            while (token == null || !token.hasMoreTokens()) {
                try {
                    token = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return token.nextToken();
        }

        public int ni() {
            return Integer.parseInt(sn());
        }

        public String snl() throws IOException {
            return br.readLine();
        }

        public long nlo() {
            return Long.parseLong(sn());
        }

        public double nd() {
            return Double.parseDouble(sn());
        }

        public int[] na(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = sc.ni();
            return a;
        }

        public long[] nal(int n) {
            long a[] = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = sc.nlo();
            return a;
        }
    }

    static int inf = 5000013;

    static class solver {
        DecimalFormat df = new DecimalFormat("0.00000000000000000000");
        long max = 1000000000000000l, min = -10000000000000000l;
        long mod = (long) 1e9 + 7;

        long pro(long a, long b) {
            return (a % mod * b % mod) % mod;
        }

        long add(long a, long b) {
            return (a + b) % mod;
        }

        long sub(long a, long b) {
            return ((a % mod - b % mod) + mod) % mod;
        }

        long ncr3(long n) {
            return (n * (n - 1) * (n - 2)) / 6l;
        }

        long ncr2(long n) {
            return n * (n - 1) / 2l;
        }

        long ncr(long n, long r) {
            double res = 1.0;
            for (long i = n, j = r; i > (n - r); i--, j--) {
                res *= (i * 1.0) / j;
            }
            return (long) Math.ceil(res);
        }

        long modPow(long x, long y) {
            long res = 1;
            x = x % mod;
            while (y > 0) {
                if (y % 2 != 0)
                    res = pro(res, x);
                x = pro(x, x);
                y = y >> 1;
            }
            return res;
        }

        int gcd(int a, int b) {
            if (b == 0)
                return a;
            return gcd(b, a % b);
        }

        final int maxn = 200005;
        List<Integer> list[] = new ArrayList[maxn];
        int level[] = new int[maxn];
        int size[] = new int[maxn];
        int parent[] = new int[maxn];

        void dfs(int s, int p, int l) {
            parent[s] = p;
            level[s] = l;
            size[s] = 1;
            for (int v : list[s]) {
                if (v == p) continue;
                dfs(v, s, l + 1);
                size[s] += size[v];
            }
        }

        class pair implements Comparable<pair> {
            int node, l, s;

            pair(int a, int b, int c) {
                l = a;
                s = b;
                node = c;
            }

            public int compareTo(pair x) {
                if (this.l != x.l)
                    return x.l - this.l;
                return this.s - x.s;
            }
        }

        void solve() {
            int n = sc.ni();
            int k = sc.ni();
            for (int i = 0; i < n; i++)
                list[i] = new ArrayList<>();
            for (int i = 0; i < n - 1; i++) {
                int u = sc.ni() - 1;
                int v = sc.ni() - 1;
                list[u].add(v);
                list[v].add(u);
            }
            dfs(0, -1, 1);
            pair arr[] = new pair[n];
            for (int i = 0; i < n; i++) {
                arr[i] = new pair(level[i], size[i], i);
            }
            Arrays.sort(arr);
//            for (pair H : arr)
//                out.println(H.node + " " + H.l + " " + H.s);
            long ans = 0;
            for (int i = 0; i < n && k > 0; i++, k--) {
                pair t = arr[i];
                ans += t.s * (t.l - 1);
                for (int v : list[t.node]) {
                    if (v != parent[t.node]) {
//                        out.println(v + " " + i);
                        ans -= size[v] * (level[v] - 1);
                    }
                }
            }
            out.println(ans);
        }
    }
}


import java.io.*;
import java.util.*;
import static java.lang.Math.*;

public class Task {
    static Reader reader;
    static PrintWriter out;

    static List<List<Integer>> tree;
    static boolean[] visited;
    static int[] size;
    static int[] depth;

    public static int dfs(int n, int d) {
        visited[n] = true;
        int count = 1;
        for (int c : tree.get(n)) {
            if (!visited[c]) {
                count += dfs(c, d + 1);
            }
        }
        depth[n] = d;
        size[n] = count;
        return count;
    }

    public static void solve() throws IOException {
        int n = reader.nextInt();
        int k = reader.nextInt();
        tree = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            tree.add(new ArrayList<>());
        }

        for (int i = 0; i < n - 1; i++) {
            int u = reader.nextInt();
            int v = reader.nextInt();
            tree.get(u).add(v);
            tree.get(v).add(u);
        }

        visited = new boolean[n + 1];
        size = new int[n + 1];
        depth = new int[n + 1];
        dfs(1, 1);

        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = size[i + 1] - depth[i + 1];
        }

        Arrays.sort(arr);

        long value = 0;
        for (int i = n - 1; i >= k; i--) {
            value += arr[i];
        }
        out.println(value);
    }

    public static void main(String[] args) throws IOException {
        reader = new Reader(System.in);
        out = new PrintWriter(System.out);

        solve();

        reader.close();
        out.close();
    }
}

class Math {
    public static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    public static long lcm(long a, long b) {
        return a * b / gcd(a, b);
    }
}

class Reader implements Closeable {
    BufferedReader bufferedReader;
    StringTokenizer stringTokenizer;

    public Reader(InputStream source) throws IOException {
        bufferedReader = new BufferedReader(new InputStreamReader(source));
        stringTokenizer = new StringTokenizer("");
    }

    String next() throws IOException {
        while (!stringTokenizer.hasMoreTokens()) {
            stringTokenizer = new StringTokenizer(bufferedReader.readLine());
        }

        return stringTokenizer.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    @Override
    public void close() throws IOException {
        bufferedReader.close();
    }
}


import java.io.*;
import java.util.*;
import static java.lang.Math.*;

public class Task {
    static Scanner reader;
    static PrintWriter out;

    static List<List<Integer>> tree;
    static boolean[] visited;
    static int[] size;
    static int[] depth;

    public static int dfs(int n, int d) {
        visited[n] = true;
        int count = 1;
        for (int c : tree.get(n)) {
            if (!visited[c]) {
                count += dfs(c, d + 1);
            }
        }
        depth[n] = d;
        size[n] = count;
        return count;
    }

    public static void solve() throws IOException {
        int n = reader.nextInt();
        int k = reader.nextInt();
        tree = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            tree.add(new ArrayList<>());
        }

        for (int i = 0; i < n - 1; i++) {
            int u = reader.nextInt();
            int v = reader.nextInt();
            tree.get(u).add(v);
            tree.get(v).add(u);
        }

        visited = new boolean[n + 1];
        size = new int[n + 1];
        depth = new int[n + 1];
        dfs(1, 1);

        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = size[i + 1] - depth[i + 1];
        }

        Arrays.sort(arr);

        long value = 0;
        for (int i = n - 1; i >= k; i--) {
            value += arr[i];
        }
        out.println(value);
    }

    public static void main(String[] args) throws IOException {
        reader = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);

        solve();

        reader.close();
        out.close();
    }
}

class Math {
    public static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    public static long lcm(long a, long b) {
        return a * b / gcd(a, b);
    }
}

class Reader implements Closeable {
    BufferedReader bufferedReader;
    StringTokenizer stringTokenizer;

    public Reader(InputStream source) throws IOException {
        bufferedReader = new BufferedReader(new InputStreamReader(source));
        stringTokenizer = new StringTokenizer("");
    }

    String next() throws IOException {
        while (!stringTokenizer.hasMoreTokens()) {
            stringTokenizer = new StringTokenizer(bufferedReader.readLine());
        }

        return stringTokenizer.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    @Override
    public void close() throws IOException {
        bufferedReader.close();
    }
}


import java.io.*;
import java.util.*;
import static java.lang.Math.*;

public class Task {
    static Scanner reader;
    static PrintWriter out;

    static List<List<Integer>> tree;
    static boolean[] visited;
    static int[] size;
    static int[] depth;

    public static int dfs(int n, int d) {
        visited[n] = true;
        int count = 1;
        for (int c : tree.get(n)) {
            if (!visited[c]) {
                count += dfs(c, d + 1);
            }
        }
        depth[n] = d;
        size[n] = count;
        return count;
    }

    public static void solve() throws IOException {
        int n = reader.nextInt();
        int k = reader.nextInt();
        tree = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            tree.add(new ArrayList<>());
        }

        for (int i = 0; i < n - 1; i++) {
            int u = reader.nextInt();
            int v = reader.nextInt();
            tree.get(u).add(v);
            tree.get(v).add(u);
        }

        visited = new boolean[n + 1];
        size = new int[n + 1];
        depth = new int[n + 1];
        dfs(1, 1);

        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = size[i + 1] - depth[i + 1];
        }

        Arrays.sort(arr);

        long value = 0;
        for (int i = n - 1; i >= k; i--) {
            value += arr[i];
        }
        out.println(value);
    }

    public static void main(String[] args) throws IOException {
        reader = new Scanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        reader.close();
        out.close();
    }
}

class Math {
    public static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    public static long lcm(long a, long b) {
        return a * b / gcd(a, b);
    }
}

class Reader implements Closeable {
    BufferedReader bufferedReader;
    StringTokenizer stringTokenizer;

    public Reader(InputStream source) throws IOException {
        bufferedReader = new BufferedReader(new InputStreamReader(source));
        stringTokenizer = new StringTokenizer("");
    }

    String next() throws IOException {
        while (!stringTokenizer.hasMoreTokens()) {
            stringTokenizer = new StringTokenizer(bufferedReader.readLine());
        }

        return stringTokenizer.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    @Override
    public void close() throws IOException {
        bufferedReader.close();
    }
}


import java.io.*;
import java.util.*;
import static java.lang.Math.*;

public class Task {
    static Reader reader;
    static PrintWriter out;

    static List<List<Integer>> tree;
    static boolean[] visited;
    static int[] size;
    static int[] depth;

    public static int dfs(int n, int d) {
        visited[n] = true;
        int count = 1;
        for (int c : tree.get(n)) {
            if (!visited[c]) {
                count += dfs(c, d + 1);
            }
        }
        depth[n] = d;
        size[n] = count;
        return count;
    }

    public static void solve() throws IOException {
        int n = reader.nextInt();
        int k = reader.nextInt();
        tree = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            tree.add(new ArrayList<>());
        }

        for (int i = 0; i < n - 1; i++) {
            int u = reader.nextInt();
            int v = reader.nextInt();
            tree.get(u).add(v);
            tree.get(v).add(u);
        }

        visited = new boolean[n + 1];
        size = new int[n + 1];
        depth = new int[n + 1];
        dfs(1, 1);

        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = size[i + 1] - depth[i + 1];
        }

        Arrays.sort(arr);

        int value = 0;
        for (int i = n - 1; i >= k; i--) {
            value += arr[i];
        }
        out.println(value);
    }

    public static void main(String[] args) throws IOException {
        reader = new Reader(System.in);
        out = new PrintWriter(System.out);

        solve();

        reader.close();
        out.close();
    }
}

class Math {
    public static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    public static long lcm(long a, long b) {
        return a * b / gcd(a, b);
    }
}

class Reader implements Closeable {
    BufferedReader bufferedReader;
    StringTokenizer stringTokenizer;

    public Reader(InputStream source) throws IOException {
        bufferedReader = new BufferedReader(new InputStreamReader(source));
        stringTokenizer = new StringTokenizer("");
    }

    String next() throws IOException {
        while (!stringTokenizer.hasMoreTokens()) {
            stringTokenizer = new StringTokenizer(bufferedReader.readLine());
        }

        return stringTokenizer.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    @Override
    public void close() throws IOException {
        bufferedReader.close();
    }
}


import com.sun.org.apache.xerces.internal.xs.ItemPSVI;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Solution {

    private static final String NO = "NO";
    private static final String YES = "YES";
    public static BufferedWriter bw;
    public static FR sc;
    private static final int MOD = 1000000007;

    public static void main(String[] args) throws Exception {
        FR sc = new FR();
        bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = 1;
        while (t > 0) {
            t--;
            int n = sc.i();
            int k = sc.i();
            Node[] nodes = new Node[n];
            for (int i = 0; i < n; i++) {
                nodes[i] = new Node(i);

            }
            for (int i = 0; i < n - 1; i++) {
                int x = sc.i();
                x--;
                int y = sc.i();
                y--;
                nodes[x].addAdj(nodes[y]);
                nodes[y].addAdj(nodes[x]);
            }
            long ans = solve(nodes, n, n - k);
            bw.write(ans + "\n");
        }
        bw.close();
    }

    private static long solve(Node[] nodes, int n, int k) {
        updateChilds(nodes, n);
        Arrays.sort(nodes, (n1, n2) -> n2.childs - n1.childs);
        for (int i = 0; i < k; i++) {
            nodes[i].tourist = true;
        }
        for (Node nod : nodes) {
            nod.vis = false;
        }
        updateChilds(nodes, n);
        long ans = 0;
        for (int i = 0; i < k; i++) {
            ans += nodes[i].childs;
        }

        return ans;
    }

    private static void updateChilds(Node[] nodes, int n) {
        for (int i = 0; i < n; i++) {
            dfs(nodes[i], n);
        }
    }

    private static int dfs(Node node, int n) {
        if (node.vis) return 0;
        node.vis = true;
        int ans = 0;
        for (Node ad : node.adj) {
            ans += dfs(ad, n);
        }
        node.childs = ans;
        if (node.tourist) return ans;
        return ans + 1;
    }

}

class Node {
    int x;
    List<Node> adj;
    int childs;
    boolean vis;
    boolean tourist;

    public Node(int x) {
        this.x = x;
        adj = new LinkedList<>();
    }

    public void addAdj(Node node) {
        adj.add(node);
    }
}

class FR {
    BufferedReader br;
    StringTokenizer st;

    public FR() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    public String s() {
        if (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {

            }
        }
        return st.nextToken();
    }

    public int i() {
        return Integer.parseInt(s());
    }

    public int[] ia(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = i();
        }
        return a;
    }

    public long[] la(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = l();
        }
        return a;
    }

    public String[] sa(int n) {
        String[] a = new String[n];
        for (int i = 0; i < n; i++) {
            a[i] = s();
        }
        return a;
    }

    public long l() {
        return Long.parseLong(s());
    }
}


import java.io.*;
import java.util.*;

public class Solution {

    private static final String NO = "NO";
    private static final String YES = "YES";
    public static BufferedWriter bw;
    public static FR sc;
    private static final int MOD = 1000000007;

    public static void main(String[] args) throws Exception {
        FR sc = new FR();
        bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = 1;
        while (t > 0) {
            t--;
            int n = sc.i();
            int k = sc.i();
            Node[] nodes = new Node[n];
            for (int i = 0; i < n; i++) {
                nodes[i] = new Node(i);

            }
            for (int i = 0; i < n - 1; i++) {
                int x = sc.i();
                x--;
                int y = sc.i();
                y--;
                nodes[x].addAdj(nodes[y]);
                nodes[y].addAdj(nodes[x]);
            }
            long ans = solve(nodes, n, n - k);
            bw.write(ans + "\n");
        }
        bw.close();
    }

    private static long solve(Node[] nodes, int n, int k) {
        updateChilds(nodes, n);
        for (Node node : nodes) {
            node.happiness = node.childs - node.depth;
        }
        Arrays.sort(nodes, (n1, n2) -> n2.happiness - n1.happiness);
        long ans = 0;
        for (int i = 0; i < k; i++) {
            ans += nodes[i].happiness;
        }
        return ans;
    }

    private static void updateChilds(Node[] nodes, int n) {
        for (int i = 0; i < n; i++) {
            dfs(nodes[i], 0);
        }
    }

    private static int dfs(Node node, int depth) {
        if (node.vis) return 0;
        node.vis = true;
        int ans = 0;
        node.depth = depth;
        for (Node ad : node.adj) {
            ans += dfs(ad, depth + 1);
        }
        node.childs = ans;
        return ans + 1;
    }

}

class Node {
    int x;
    List<Node> adj;
    int childs;
    boolean vis;
    int depth;
    int happiness;

    public Node(int x) {
        this.x = x;
        adj = new LinkedList<>();
    }

    public void addAdj(Node node) {
        adj.add(node);
    }
}

class FR {
    BufferedReader br;
    StringTokenizer st;

    public FR() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    public String s() {
        if (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {

            }
        }
        return st.nextToken();
    }

    public int i() {
        return Integer.parseInt(s());
    }

    public int[] ia(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = i();
        }
        return a;
    }

    public long[] la(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = l();
        }
        return a;
    }

    public String[] sa(int n) {
        String[] a = new String[n];
        for (int i = 0; i < n; i++) {
            a[i] = s();
        }
        return a;
    }

    public long l() {
        return Long.parseLong(s());
    }
}


import java.io.*;
import java.util.*;

public class Main{
	//-----------PrintWriter for faster output---------------------------------
	public static PrintWriter out;

	public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));

        int n = sc.nextInt();
        int k = sc.nextInt();

        ArrayList<Integer>[] G = new ArrayList[n];
        for (int i = 0; i < n; i++) {
        	G[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < n - 1; i++) {
        	int u = sc.nextInt() - 1;
        	int v = sc.nextInt() - 1;
        	G[u].add(v);
        	G[v].add(u);
        }

        int[] depth = new int[n];
        int[] size = new int[n];
        dfs(0, depth, size, G);

        ArrayList<Integer> solution = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
        	solution.add(depth[i] - size[i]);
        }
        Collections.sort(solution);
        long sol = 0;
        for (int i = 1; i <= k; i++) {
        	sol += solution.get(n - i);
        }

        out.println(sol);
        out.close();
   }

	private static void dfs(int u, int[] depth, int[] size, ArrayList<Integer>[] G) {
		for (int v : G[u]) {
			if (depth[v] == 0 && v != 0) {
				depth[v] = depth[u] + 1;
				dfs(v, depth, size, G);
				size[u] += size[v] + 1;
			}
		}
	}

}

//-----------MyScanner class for faster input----------
class MyScanner {
      BufferedReader br;
      StringTokenizer st;

      public MyScanner() {
         br = new BufferedReader(new InputStreamReader(System.in));
      }

      String next() {
          while (st == null || !st.hasMoreElements()) {
              try {
                  st = new StringTokenizer(br.readLine());
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return st.nextToken();
      }

      int nextInt() {
          return Integer.parseInt(next());
      }

      long nextLong() {
          return Long.parseLong(next());
      }

      double nextDouble() {
          return Double.parseDouble(next());
      }

      String nextLine(){
          String str = "";
	  try {
	     str = br.readLine();
	  } catch (IOException e) {
	     e.printStackTrace();
	  }
	  return str;
      }
}


import java.io.*;
import java.util.*;

public class Main{
	//-----------PrintWriter for faster output---------------------------------
	public static PrintWriter out;

	public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));

        int n = sc.nextInt();
        int k = sc.nextInt();

        ArrayList<Integer>[] G = new ArrayList[n];
        for (int i = 0; i < n; i++) {
        	G[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < n - 1; i++) {
        	int u = sc.nextInt() - 1;
        	int v = sc.nextInt() - 1;
        	G[u].add(v);
        	G[v].add(u);
        }

        int[] depth = new int[n];
        int[] size = new int[n];

        dfs(0, depth, size, G);

        ArrayList<Integer> solution = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
        	solution.add(depth[i] - size[i]);
        }
        Collections.sort(solution);
        int sol = 0;
        for (int i = n-1; i >= n-k; i--) {
        	sol += solution.get(i);
        }

        out.println(sol);
        out.close();
   }

	private static void dfs(int u, int[] depth, int[] size, ArrayList<Integer>[] G) {
		for (int v : G[u]) {
			if (depth[v] == 0 && v != 0) {
				depth[v] = depth[u] + 1;
				dfs(v, depth, size, G);
				size[u] += size[v] + 1;
			}
		}
	}

}

//-----------MyScanner class for faster input----------
class MyScanner {
      BufferedReader br;
      StringTokenizer st;

      public MyScanner() {
         br = new BufferedReader(new InputStreamReader(System.in));
      }

      String next() {
          while (st == null || !st.hasMoreElements()) {
              try {
                  st = new StringTokenizer(br.readLine());
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          return st.nextToken();
      }

      int nextInt() {
          return Integer.parseInt(next());
      }

      long nextLong() {
          return Long.parseLong(next());
      }

      double nextDouble() {
          return Double.parseDouble(next());
      }

      String nextLine(){
          String str = "";
	  try {
	     str = br.readLine();
	  } catch (IOException e) {
	     e.printStackTrace();
	  }
	  return str;
      }
}


import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.math.*;

public class linovakingdom {
    static int n, k;
    static LinkedList<Integer>[] g;
    static int[][] dp;
    public static void main(String[] uselessScrub) throws Exception {
        FastScanner in = new FastScanner();
        FastPrinter out = new FastPrinter();

        n=in.nextInt(); k=in.nextInt();
        g = new LinkedList[n];
        dp=new int[n][2];
        for (int i=0; i<n; i++) g[i]=new LinkedList<>();

        for (int i=0; i<n-1; i++) {
            int a=in.nextInt()-1, b=in.nextInt()-1;
            g[a].add(b);
            g[b].add(a);
        }
        dfs(0, 0);
        Arrays.sort(dp, (int[] a, int[] b) -> (b[0]-b[1])-(a[0]-a[1]));
        long ans=0;
        for (int i=0; i<k; i++) {
            ans+=dp[i][0]-dp[i][1];
        }

        out.println(ans);
        out.close();
    }

    public static void dfs(int a, int b) {
        for (int t:g[a]) if (t!=b) {
            dp[t][0]=dp[a][0]+1;
            dfs(t, a);
            dp[a][1]+=dp[t][1]+1;
        }
    }

    private static class FastPrinter {
        static final char ENDL = '\n';
        StringBuilder buf;
        PrintWriter pw;

        public FastPrinter() {
            buf = new StringBuilder();
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        }

        public FastPrinter(OutputStream stream) {
            buf = new StringBuilder();
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream)));
        }

        public FastPrinter(String fileName) throws Exception {
            buf = new StringBuilder();
            pw = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
        }

        public FastPrinter(StringBuilder buf) {
            this.buf = buf;
        }

        public void print(int a) {
            buf.append(a);
        }

        public void print(long a) {
            buf.append(a);
        }

        public void print(char a) {
            buf.append(a);
        }

        public void print(char[] a) {
            buf.append(a);
        }

        public void print(double a) {
            buf.append(a);
        }

        public void print(String a) {
            buf.append(a);
        }

        public void print(Object a) {
            buf.append(a.toString());
        }

        public void println() {
            buf.append(ENDL);
        }

        public void println(int a) {
            buf.append(a);
            buf.append(ENDL);
        }

        public void println(long a) {
            buf.append(a);
            buf.append(ENDL);
        }

        public void println(char a) {
            buf.append(a);
            buf.append(ENDL);
        }

        public void println(char[] a) {
            buf.append(a);
            buf.append(ENDL);
        }

        public void println(double a) {
            buf.append(a);
            buf.append(ENDL);
        }

        public void println(String a) {
            buf.append(a);
            buf.append(ENDL);
        }

        public void println(Object a) {
            buf.append(a.toString());
            buf.append(ENDL);
        }

        public void close() {
            pw.print(buf);
            pw.close();
        }

        public void flush() {
            pw.print(buf);
            pw.flush();
            buf.setLength(0);
        }

    }

    private static class FastScanner {
        final private int BUFFER_SIZE = 1 << 10;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public FastScanner() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public FastScanner(InputStream stream) {
            din = new DataInputStream(stream);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public FastScanner(String fileName) throws IOException {
            Path p = Paths.get(fileName);
            buffer = Files.readAllBytes(p);
            bytesRead = buffer.length;
        }

        int[] nextIntArray(int N) throws IOException {
            int[] arr = new int[N];
            for (int i = 0; i < N; i++) arr[i] = nextInt();
            return arr;
        }

        int[][] nextDoubleIntArray(int n, int m) throws IOException {
            int[][] arr = new int[n][m];
            for (int i=0; i<n; i++) for (int j=0; j<m; j++) arr[i][j]=nextInt();
            return arr;
        }

        String nextLine() throws IOException {
            int c = read();
            while (c != -1 && isEndline(c))
                c = read();
            if (c == -1) {
                return null;
            }
            StringBuilder res = new StringBuilder();
            do {
                if (c >= 0) {
                    res.appendCodePoint(c);
                }
                c = read();
            } while (!isEndline(c));
            return res.toString();
        }

        boolean isEndline(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

        String next() throws Exception {
            int c = readOutSpaces();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg) return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg) return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg) c = read();

            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg) return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }

        private int readOutSpaces() throws IOException {
            while (true) {
                if (bufferPointer == bytesRead) fillBuffer();
                int c = buffer[bufferPointer++];
                if (!isSpaceChar(c)) {
                    return c;
                }
            }
        }

        public void close() throws IOException {
            if (din == null) return;
            din.close();
        }
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskA {
        ArrayList<Integer> es;
        ArrayList<Integer> profits = new ArrayList<>();

        int dfs(int v, int prev, int depth) {
            int size = 1;
            for (int to : es[v]) {
                if (to == prev) {
                    continue;
                }
                size += dfs(to, v, depth + 1);
            }
            int profit = (size - 1) - depth;
//        System.err.println((v + 1) + " " + profit);
            profits.add(profit);
            return size;
        }

        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();
            es = new ArrayList[n];
            for (int i = 0; i < n; i++) {
                es[i] = new ArrayList<>();
            }
            for (int it = 0; it < n - 1; it++) {
                int u = in.nextInt() - 1;
                int v = in.nextInt() - 1;
                es[u].add(v);
                es[v].add(u);
            }
            dfs(0, -1, 0);
            Collections.sort(profits);
            Collections.reverse(profits);
            long ans = 0;
            for (int i = 0; i < n - k; i++) {
                ans += profits.get(i);
            }
            out.println(ans);
        }

    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream is) {
            this.br = new BufferedReader(new InputStreamReader(is));
        }

        public FastScanner(File f) {
            try {
                this.br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskA {
        ArrayList<Integer>[] es;
        ArrayList<Integer> profits = new ArrayList<>();

        int dfs(int v, int prev, int depth) {
            int size = 1;
            for (int to : es[v]) {
                if (to == prev) {
                    continue;
                }
                size += dfs(to, v, depth + 1);
            }
            int profit = (size - 1) - depth;
//        System.err.println((v + 1) + " " + profit);
            profits.add(profit);
            return size;
        }

        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();
            es = new ArrayList[n];
            for (int i = 0; i < n; i++) {
                es[i] = new ArrayList<>();
            }
            for (int it = 0; it < n - 1; it++) {
                int u = in.nextInt() - 1;
                int v = in.nextInt() - 1;
                es[u].add(v);
                es[v].add(u);
            }
            dfs(0, -1, 0);
            Collections.sort(profits);
            Collections.reverse(profits);
            long ans = 0;
            for (int i = 0; i < n - k; i++) {
                ans += profits.get(i);
            }
            out.println(ans);
        }

    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream is) {
            this.br = new BufferedReader(new InputStreamReader(is));
        }

        public FastScanner(File f) {
            try {
                this.br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;

public class LinovaAndKingdom_1336A {
	static tree[] t, node;
	static int n, k;
	static boolean[] check;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		n = scan.nextInt(); // city
		k = scan.nextInt(); // industry
		t = new tree[n + 1];
		node = new tree[n + 1];
		check = new boolean[n + 1];

		for (int i = 0; i < n + 1; i++) {
			t[i] = new tree();
			node[i] = new tree();
		}
		int a, b;
		for (int i = 0; i < n - 1; i++) {
			a = scan.nextInt();
			b = scan.nextInt();
			t[a].add(b);
			t[b].add(a);
		}
		DFS(1);
		// child list 완성

		int[] result = new int[n + 1];
		long ans = 0;
		for (int i = 0; i < n ; i++) {
			result[i] = node[i+1].depth - node[i+1].cNum;
			//System.out.println(node[i+1].depth + "/" + node[i+1].cNum);
		}

		Arrays.sort(result);

		for (int i = 0; i < k; i++) {
			ans += result[n - i];
		}
		System.out.println(ans);
	}

	public static int DFS(int pos) {
		int count = 0;

		check[pos] = true;
		for (int i = 0; i < t[pos].cNum; i++) {
			if (check[t[pos].child.get(i)] == false) {
				node[t[pos].child.get(i)].depth = (node[pos].depth) + 1;
				count += DFS(t[pos].child.get(i));
			}
		}
		node[pos].cNum = count;
		return count + 1;
	}
}

class tree {
	int depth = 0;
	int cNum = 0;
	Vector<Integer> child = new Vector();

	public tree() {
	}

	public void add(int c) {
		cNum++;
		child.add(c);
	}
}


import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;

public class LinovaAndKingdom_1336A {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int n = scan.nextInt(); // city
		int k = scan.nextInt(); // industry
		tree[] t = new tree[n + 1];

		for (int i = 0; i < n + 1; i++) {
			t[i] = new tree();
		}
		int a, b;
		for (int i = 0; i < n-1; i++) {
			a = scan.nextInt();
			b = scan.nextInt();
			t[a].add(b);
			t[b].add(a);
		}
		Vector<Integer> stack = new Vector();
		stack.add(1);
		boolean[] check = new boolean[n + 1];
		while (stack.size() != 0) {
			int s = stack.lastElement();
			check[s] = true;
			stack.remove(stack.size() - 1);
			for (int i = 0; i < t[s].cNum; i++) {
				//System.out.println(t[s].cNum+"::"+t[s].child.get(i));
				if (check[t[s].child.get(i)] == true) {
					t[s].child.remove(i);
					t[s].cNum--;
					i--;
				} else {
					stack.add(t[s].child.get(i));
					t[t[s].child.get(i)].depth = t[s].depth +1;
				}
			}
			//System.out.println(stack);
		}
		// child list 완성
		int[] result = new int[n+1];
		for(int i=1;i<n+1;i++) {
			result[i] = t[i].depth - t[i].cNum;
			//System.out.println(t[i].depth+"/"+t[i].cNum);
		}
		Arrays.sort(result);
		int ans = 0;
		for(int i=0;i<k;i++) {
			ans += result[n-i];
		}
		System.out.println(ans);
		scan.close();
	}

}

class tree {
	int depth = 0;
	int cNum = 0;
	Vector<Integer> child = new Vector();

	public tree() {
	}

	public void add(int c) {
		cNum++;
		child.add(c);
	}
}


import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;

public class LinovaAndKingdom_1336A {
	static tree[] t, node;
	static int n, k;
	static boolean[] check;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		n = scan.nextInt(); // city
		k = scan.nextInt(); // industry
		t = new tree[n + 1];
		node = new tree[n + 1];
		check = new boolean[n + 1];

		for (int i = 0; i < n + 1; i++) {
			t[i] = new tree();
			node[i] = new tree();
		}
		int a, b;
		for (int i = 0; i < n - 1; i++) {
			a = scan.nextInt();
			b = scan.nextInt();
			t[a].add(b);
			t[b].add(a);
		}
		DFS(1);
		// child list 완성

		int[] result = new int[n + 1];
		int ans = 0;
		for (int i = 1; i < n + 1; i++) {
			result[i] = node[i].depth - node[i].cNum;
			//System.out.println(node[i].depth + "/" + node[i].cNum);

		}

		Arrays.sort(result);

		for (int i = 0; i < k; i++) {
			ans += result[n - i];
		}
		System.out.println(ans);
	}

	public static int DFS(int pos) {
		int count = 0;

		check[pos] = true;
		for (int i = 0; i < t[pos].cNum; i++) {
			if (check[t[pos].child.get(i)] == false) {
				node[t[pos].child.get(i)].depth = (node[pos].depth) + 1;
				count += DFS(t[pos].child.get(i));
			}
		}
		node[pos].cNum = count;
		return count + 1;
	}
}

class tree {
	int depth = 0;
	int cNum = 0;
	Vector<Integer> child = new Vector();

	public tree() {
	}

	public void add(int c) {
		cNum++;
		child.add(c);
	}
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int Long;
typedef long double Double;
const int MAXN = 1e5 + 4;
const int MAXLG = 21;

int n, p[MAXN], B[MAXN], T[MAXLG][MAXN];
set<int> S[MAXN];

void update(int i, int val) {
	while(i <= n) {
		B[i] += val;
		i += (i & -i);
	}
}
int get(int i) {
	int sum = 0;
	while(i) {
		sum += B[i];
		i -= (i & -i);
	}
	return sum;
}
int get(int i, int j) {
	if(i > j) return 0;
	return get(j) - get(i - 1);
}
void build() {
	for(int i = 1; i <= n; ++i)
		T[0][i] = p[i];
	for(int i = 1; (1 << i) <= n; ++i)
		for(int j = 1; j + (1 << (i - 1)) <= n; ++j)
			T[i][j] = max(T[i - 1][j], T[i - 1][j + (1 << (i - 1))]);
}
int query(int i, int j) {
	if(i > j) return 0;
	int diff = j - i + 1, k = MAXLG;
	while((1 << k) > diff) --k;
	return max(T[k][i], T[k][j - (1 << k) + 1]);
}

int main() {
	memset(B, 0, sizeof(B));
	memset(T, 0, sizeof(T));

	scanf("%d", &n);
	for(int i = 1; i <= n; ++i)
		scanf("%d", p + i);
	build();

	for(int i = 1; i <= n; ++i) {
		if(get(p[i] + 1, n) == 1) {
			int badMatch = query(1, i - 1);
			S[badMatch].insert(p[i]);
		}
		update(p[i], 1);
	}

	int best = -1, ans = 0;
	for(int i = 1; i <= n; ++i) {
		// printf("%d) %d\n", i, (int) S[i].size());
		if(best < (int) S[i].size()) {
			best = (int) S[i].size();
			ans = i;
		}
	}

	printf("%d\n", ans);
	return 0;
}