Would you mind sharing the business case that would use such a strange data structure?
Regarding your requirement:
Am I right that you'd need to start a new group as soon as neither one of the numbers in one row has been previously assigned to a group?
If so, how would you handle row 6 to be "6 | 101 | 104" instead of "6 | 106 | 104" (both values in a row are preassigned, but to different groups)?
Are the groups supposed to be consecutive or can you "jump back" to let's say group 1 if row 8 would be 8 | 101 | 109?
Again: really strange requirement...