6和8的最小公倍数是24,只要24就买3个8,其余几个值单列出来:

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Integer> v = new ArrayList<>(Arrays.asList(0, 6, 8, 12, 14, 16, 20, 22));
        List<Integer> r = new ArrayList<>(Arrays.asList(0, 1, 1, 2, 2, 2, 3, 3));
        int ans = n/24*3;
        if(v.contains(n%24)) System.out.println(ans + r.get(v.indexOf(n%24)));
        else System.out.println(-1);
    }
}