Flatten Nested AD groups using powershell scripting

Working on this script for longtime with no improvement

# Specify the DistinguishedName (DN) of the parent group.
# We will remove all members of this group that have class "group".
$GroupDN = "cn=Sales,ou=West,dc=domain,dc=com"
# The Member property is an array of the DistinguishedNames (DN) of all direct members of the group.
# The DN is required by Get-ADObject, which we use to determine the class of each member.
$Members = (Get-ADGroup -Identity $GroupDN -Properties Member).Member

# Create an array of distinguished names of nested groups to remove.
$GroupsToRemove = @()

# Enumerate all direct members of the parent group and determine which are nested groups.
ForEach ($Member In $Members)
{
    # Members can be users, computers, contacts, or nested groups. We only consider groups.
    $Class = (Get-ADObject -Identity $Member).ObjectClass
    If ($Class -eq "group")
    {
        # Add the DN of this nested group to the array of groups to remove.
        $GroupsToRemove = $GroupsToRemove + $Member
    }
}

# Remove any nested groups from the parent group.
If ($GroupsToRemove.Count -gt 0)
{
    Set-ADGroup -Identity $GroupDN -Remove @{Member=$GroupsToRemove}
}
.This script fabulously flattens any given levels of nested groups to individual independent AD groups.
But i want to keep the direct first level of nested AD group within group and the rest nested groups should be removed.

Eg. 1st level GroupA nested to GroupB, 2nd level GroupB nested to GroupC, 3rd level GroupC nested to GroupD, 4th level GroupE nested to GroupF

here i wanted retain GroupA nesting with Group B but needs to denests(flatten) all other levels of nesting