<p>You are given two arrays of integers <code>nums1</code> and <code><font face="monospace">nums2</font></code>, possibly of different lengths. The values in the arrays are between <code>1</code> and <code>6</code>, inclusive.</p>

<p>In one operation, you can change any integer&#39;s value in <strong>any </strong>of the arrays to <strong>any</strong> value between <code>1</code> and <code>6</code>, inclusive.</p>

<p>Return <em>the minimum number of operations required to make the sum of values in </em><code>nums1</code><em> equal to the sum of values in </em><code>nums2</code><em>.</em> Return <code>-1</code>​​​​​ if it is not possible to make the sum of the two arrays equal.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed.
- Change nums2[0] to 6. nums1 = [1,2,3,4,5,6], nums2 = [<u><strong>6</strong></u>,1,2,2,2,2].
- Change nums1[5] to 1. nums1 = [1,2,3,4,5,<strong><u>1</u></strong>], nums2 = [6,1,2,2,2,2].
- Change nums1[2] to 2. nums1 = [1,2,<strong><u>2</u></strong>,4,5,1], nums2 = [6,1,2,2,2,2].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,1,1,1,1,1,1], nums2 = [6]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no way to decrease the sum of nums1 or to increase the sum of nums2 to make them equal.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [6,6], nums2 = [1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can make the sums of nums1 and nums2 equal with 3 operations. All indices are 0-indexed. 
- Change nums1[0] to 2. nums1 = [<strong><u>2</u></strong>,6], nums2 = [1].
- Change nums1[1] to 2. nums1 = [2,<strong><u>2</u></strong>], nums2 = [1].
- Change nums2[0] to 4. nums1 = [2,2], nums2 = [<strong><u>4</u></strong>].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 6</code></li>
</ul>

class Solution:
    def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
        
        if sum(nums1) > sum(nums2):
            nums1, nums2 = nums2, nums1

        # we want to pop the largest gain, so the gains have to be negative
        gains1 = [-(6 - number) for number in nums1]
        heapq.heapify(gains1)
        gains2 = [-(number - 1) for number in nums2]
        heapq.heapify(gains2)

        nums1_sum = sum(nums1)
        nums2_sum = sum(nums2)
        difference = nums2_sum - nums1_sum

        operations = 0
        while difference > 0 and (gains1 or gains2):
            gain1 = -gains1[0] if gains1 else float("-inf")
            gain2 = -gains2[0] if gains2 else float("-inf")

            if gain1 >= gain2:
                heapq.heappop(gains1)
                difference -= gain1
            else:
                heapq.heappop(gains2)
                difference -= gain2

            operations += 1

        if difference > 0:
            return -1
        return operations

        """
        if sum(nums1) > sum(nums2):
            nums1, nums2 = nums2, nums1

        heap1 = [(number, index) for (index, number) in enumerate(nums1)]
        heapq.heapify(heap1)
        heap2 = [(-number, index) for (index, number) in enumerate(nums2)]
        heapq.heapify(heap2)

        nums1_sum = sum(nums1)
        nums2_sum = sum(nums2)

        operations = 0
        while nums1_sum < nums2_sum and (heap1 or heap2):
            difference = nums2_sum - nums1_sum

            number1 = heap1[0][0] if heap1 else float("-inf")
            number2 = -heap2[0][0] if heap2 else float("-inf")

            if abs(difference - (6 - number1)) <= abs(difference - (number2 - 1)):
                number1, index = heapq.heappop(heap1)
                nums1[index] = min(difference + number1, 6)
                nums1_sum += nums1[index] - number1
            else:
                negative_number2, index = heapq.heappop(heap2)
                nums2[index] = max(number2 - difference, 1)
                nums2_sum -= number2 - nums2[index]

            operations += 1

        if nums1_sum == nums2_sum:
            return operations
        return -1
        """
            
1034